Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 布局未在不同设备上正确显示_Java_Android_Android Layout_Android Cardview - Fatal编程技术网

Java 布局未在不同设备上正确显示

Java 布局未在不同设备上正确显示,java,android,android-layout,android-cardview,Java,Android,Android Layout,Android Cardview,我已经创建了一个应用程序,其中我采取线性布局,并把2卡德维尤内。该布局在我的1080p设备上看起来非常完美,但在分辨率较低的设备上无法正确显示。 以下是该应用程序的屏幕截图,低分辨率设备上缺少一些内容。我已经使用sp和dp作为文本和页边距,但仍然存在问题。我有什么遗漏吗 <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent"

我已经创建了一个应用程序,其中我采取线性布局,并把2卡德维尤内。该布局在我的1080p设备上看起来非常完美,但在分辨率较低的设备上无法正确显示。 以下是该应用程序的屏幕截图,低分辨率设备上缺少一些内容。我已经使用sp和dp作为文本和页边距,但仍然存在问题。我有什么遗漏吗

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:padding="2dp">
            <android.support.v7.widget.CardView
                android:layout_width="175dp"
                android:layout_height="200dp"
                android:id="@+id/time_table"
                android:layout_margin="10dp">
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical"
                    android:padding="10dp"
                    android:background="@color/purple"
                    android:gravity="center">

                    <ImageView
                        android:layout_width="75dp"
                        android:layout_height="75dp"
                        android:src="@drawable/ic_time_table_black_24dp"/>

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textColor="#fff"
                        android:padding="5dp"
                        android:textStyle="bold"
                        android:fontFamily="serif"
                        android:text="Time Table"/>

                    <View
                        android:layout_width="match_parent"
                        android:layout_height="2dp"
                        android:background="#ffffff"
                        />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:padding="5dp"
                        android:textColor="#fff"
                        android:text="Check your time table here.."/>

                </LinearLayout>

            </android.support.v7.widget.CardView>


尝试使用这样的结构(使用图像元素而不是文本视图,因为您的要求是必须使用布局\权重属性):-


以上答案将解决您的问题
但如果您不想使用
约束布局
和其他布局,如
相对布局
线性布局
布局,那么我建议对此使用多维度文件

它将创建具有相同名称的文件夹

您的解决方案就在这里:

在这种情况下,可以使用布局权重来维护布局

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    android:padding="2dp">

    <android.support.v7.widget.CardView
        android:id="@+id/time_table"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="200dp"
                android:background="@color/colorPrimary"
                android:gravity="center"
                android:layout_weight="1"
                android:orientation="vertical"
                android:padding="10dp">

                <ImageView
                    android:layout_width="75dp"
                    android:layout_height="75dp"
                    android:src="@drawable/ic_edit" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:fontFamily="serif"
                    android:padding="5dp"
                    android:text="Time Table"
                    android:textColor="#fff"
                    android:textStyle="bold" />

                <View
                    android:layout_width="match_parent"
                    android:layout_height="2dp"
                    android:background="#ffffff" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:padding="5dp"
                    android:text="Check your time table here.."
                    android:textColor="#fff" />

            </LinearLayout>

        </LinearLayout>

    </android.support.v7.widget.CardView>

    <LinearLayout>

imagview、textview需要图像和文本可以更改,它应该可以工作

 <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:weightSum="2">


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:layout_weight="1"
            android:orientation="horizontal"
            android:weightSum="2">

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="#123455"
                android:gravity="center"
                android:orientation="vertical"
                android:padding="10dp">

                <ImageView
                    android:layout_width="75dp"
                    android:layout_height="75dp"
                    android:src="@drawable/ic_message" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:fontFamily="serif"
                    android:padding="5dp"
                    android:text="Time Table"
                    android:textColor="#fff"
                    android:textStyle="bold" />

                <View
                    android:layout_width="match_parent"
                    android:layout_height="2dp"
                    android:background="#ffffff" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:padding="5dp"
                    android:text="Check your time table here.."
                    android:textColor="#fff" />


            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_marginLeft="8dp"
                android:layout_weight="1"
                android:background="#123455"
                android:gravity="center"
                android:orientation="vertical"
                android:padding="10dp">

                <ImageView
                    android:layout_width="75dp"
                    android:layout_height="75dp"
                    android:src="@drawable/ic_message" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:fontFamily="serif"
                    android:padding="5dp"
                    android:text="Time Table"
                    android:textColor="#fff"
                    android:textStyle="bold" />

                <View
                    android:layout_width="match_parent"
                    android:layout_height="2dp"
                    android:background="#ffffff" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:padding="5dp"
                    android:text="Check your time table here.."
                    android:textColor="#fff" />


            </LinearLayout>


        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_weight="1"
            android:orientation="horizontal"
            android:weightSum="2">

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="#123455"
                android:gravity="center"
                android:orientation="vertical"
                android:padding="10dp">

                <ImageView
                    android:layout_width="75dp"
                    android:layout_height="75dp"
                    android:src="@drawable/ic_message" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:fontFamily="serif"
                    android:padding="5dp"
                    android:text="Time Table"
                    android:textColor="#fff"
                    android:textStyle="bold" />

                <View
                    android:layout_width="match_parent"
                    android:layout_height="2dp"
                    android:background="#ffffff" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:padding="5dp"
                    android:text="Check your time table here.."
                    android:textColor="#fff" />


            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_marginLeft="8dp"
                android:layout_weight="1"
                android:background="#123455"
                android:gravity="center"

                android:orientation="vertical"
                android:padding="10dp">

                <ImageView
                    android:layout_width="75dp"
                    android:layout_height="75dp"
                    android:src="@drawable/ic_message" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:fontFamily="serif"
                    android:padding="5dp"
                    android:text="Time Table"
                    android:textColor="#fff"
                    android:textStyle="bold" />

                <View
                    android:layout_width="match_parent"
                    android:layout_height="2dp"
                    android:background="#ffffff" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:padding="5dp"
                    android:text="Check your time table here.."
                    android:textColor="#fff" />


            </LinearLayout>


        </LinearLayout>

    </LinearLayout>

正如我所想,您的视图使用了固定大小。要使布局自然缩放,而不考虑设备屏幕大小,请尽可能使用
match\u parent
wrap\u content
。只有
imageviews
是我在大多数情况下固定大小的东西,以防止图像被拉伸

尽可能多地使用
constraintlayout
。这是迄今为止最好的布局。我不再使用
relativelayout
。如果您需要创建两个并排视图以获得相同的大小,一个
表格行就足够了。我不认为这是最好的做法,因为这不是谷歌想要做的平面布局,但这是我现在唯一知道的事情

对于
卡片视图
,您可以将它们放在
表格行
上。将
表格行的
权重总和设置为2,然后将
卡片视图的
布局权重设置为1

对于
填充
,请使用标准尺寸。我主要使用
8dp
来填充我需要的任何填充物。另外,如果你真的喜欢设计,或者想提高你在android设计方面的知识(请使用design editor,当使用
constraintlayout
时,它会让你的生活更轻松),请阅读。我不会把所有的东西都用在那里,因为我不知道怎么做,但从中知道一两件事是很好的

这就是我将如何做你的布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:weightSum="2">

        <android.support.constraint.ConstraintLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#910FF5">

            <ImageView
                android:id="@+id/imageView3"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:layout_marginEnd="8dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:srcCompat="@mipmap/ic_launcher" />

            <TextView
                android:id="@+id/textView4"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:layout_marginEnd="8dp"
                android:gravity="center"
                android:text="Time Table"
                android:textColor="@android:color/white"
                android:textSize="24sp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/imageView3" />

            <View
                android:id="@+id/view2"
                android:layout_width="wrap_content"
                android:layout_height="5dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:layout_marginEnd="8dp"
                android:background="#ffffff"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/textView4" />

            <TextView
                android:id="@+id/textView6"
                android:layout_width="0dp"
                android:layout_height="50dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:layout_marginEnd="8dp"
                android:layout_marginBottom="8dp"
                android:gravity="center"
                android:text="Check your time table here"
                android:textColor="@android:color/white"
                android:textSize="24sp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/view2" />
        </android.support.constraint.ConstraintLayout>

        <android.support.constraint.ConstraintLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#910FF5">

            <ImageView
                android:id="@+id/imageView4"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:layout_marginEnd="8dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:srcCompat="@mipmap/ic_launcher" />

            <TextView
                android:id="@+id/textView5"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:layout_marginEnd="8dp"
                android:gravity="center"
                android:text="Time Table"
                android:textColor="@android:color/white"
                android:textSize="24sp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/imageView4" />

            <View
                android:id="@+id/view3"
                android:layout_width="wrap_content"
                android:layout_height="5dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:layout_marginEnd="8dp"
                android:background="#ffffff"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/textView5" />

            <TextView
                android:id="@+id/textView7"
                android:layout_width="0dp"
                android:layout_height="50dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:layout_marginEnd="8dp"
                android:layout_marginBottom="8dp"
                android:gravity="center"
                android:text="Check your time table here"
                android:textColor="@android:color/white"
                android:textSize="24sp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/view3" />
        </android.support.constraint.ConstraintLayout>
    </TableRow>

</android.support.constraint.ConstraintLayout>


正如我所说,我不知道如何让它遵循谷歌的平面布局准则。如果有人知道怎么做,请随时编辑我的答案。对我自己也有好处。

如果您的布局没有那么大的限制,您想让它占用可用空间,我不建议使用固定尺寸。我也推荐使用constraintlayout。你能给我看看你的XML代码吗@DPrince OK..@rmanalo我将尝试使用约束布局。如果您希望应用程序布局独立于设备像素,请尽可能多地使用约束布局,并尝试对txt大小使用“sp”而不是“dp”。
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:weightSum="2">

        <android.support.constraint.ConstraintLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#910FF5">

            <ImageView
                android:id="@+id/imageView3"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:layout_marginEnd="8dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:srcCompat="@mipmap/ic_launcher" />

            <TextView
                android:id="@+id/textView4"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:layout_marginEnd="8dp"
                android:gravity="center"
                android:text="Time Table"
                android:textColor="@android:color/white"
                android:textSize="24sp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/imageView3" />

            <View
                android:id="@+id/view2"
                android:layout_width="wrap_content"
                android:layout_height="5dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:layout_marginEnd="8dp"
                android:background="#ffffff"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/textView4" />

            <TextView
                android:id="@+id/textView6"
                android:layout_width="0dp"
                android:layout_height="50dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:layout_marginEnd="8dp"
                android:layout_marginBottom="8dp"
                android:gravity="center"
                android:text="Check your time table here"
                android:textColor="@android:color/white"
                android:textSize="24sp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/view2" />
        </android.support.constraint.ConstraintLayout>

        <android.support.constraint.ConstraintLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#910FF5">

            <ImageView
                android:id="@+id/imageView4"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:layout_marginEnd="8dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:srcCompat="@mipmap/ic_launcher" />

            <TextView
                android:id="@+id/textView5"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:layout_marginEnd="8dp"
                android:gravity="center"
                android:text="Time Table"
                android:textColor="@android:color/white"
                android:textSize="24sp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/imageView4" />

            <View
                android:id="@+id/view3"
                android:layout_width="wrap_content"
                android:layout_height="5dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:layout_marginEnd="8dp"
                android:background="#ffffff"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/textView5" />

            <TextView
                android:id="@+id/textView7"
                android:layout_width="0dp"
                android:layout_height="50dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:layout_marginEnd="8dp"
                android:layout_marginBottom="8dp"
                android:gravity="center"
                android:text="Check your time table here"
                android:textColor="@android:color/white"
                android:textSize="24sp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/view3" />
        </android.support.constraint.ConstraintLayout>
    </TableRow>

</android.support.constraint.ConstraintLayout>