android - 布局内部的 LinearLayout 与父级不匹配

标签 android xml android-layout

当方向为水平、宽度为 0dp 并设置了权重时,布局内的 LinearLayout 与父级不匹配。此 XML 文件用于 RecyclerView 项目布局。布局 XML 文件如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:orientation="vertical"
                  android:layout_width="match_parent"
                  android:layout_height="150dp"
                  android:paddingTop="@dimen/screen_padding"
                  android:paddingLeft="@dimen/screen_padding"
                  android:paddingRight="@dimen/screen_padding">

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:orientation="horizontal">

        <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:gravity="center"
                android:text="粤T24833"
                android:textSize="30dp"
                android:textColor="@color/deep_gray"
                android:background="@drawable/border"
                android:id="@+id/textView9"/>

        <LinearLayout
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:orientation="vertical">

            <TextView
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:text="行驶里程(km): 10000"
                    android:textSize="@dimen/font_24"
                    android:textColor="@color/gray"
                    android:background="@drawable/border"
                    android:id="@+id/textView11"/>

            <TextView
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:text="运行时间(min): 238"
                    android:textSize="@dimen/font_24"
                    android:textColor="@color/gray"
                    android:background="@drawable/border"
                    android:id="@+id/textView10"/>
        </LinearLayout>

    </LinearLayout>

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:orientation="horizontal">

        <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:gravity="center"
                android:text="停车次数"
                android:id="@+id/textView"
                android:textSize="@dimen/font_24"
                android:textColor="@color/deep_gray"
                android:background="@drawable/border"/>

        <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:gravity="center"
                android:text="熄火次数"
                android:id="@+id/textView2"
                android:textSize="@dimen/font_24"
                android:textColor="@color/deep_gray"
                android:background="@drawable/border"/>

        <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:gravity="center"
                android:text="行驶次数"
                android:id="@+id/textView3"
                android:textSize="@dimen/font_24"
                android:textColor="@color/deep_gray"
                android:background="@drawable/border"/>


        <LinearLayout
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:orientation="vertical">

            <TextView
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:text="提速次数"
                    android:id="@+id/textView4"
                    android:textSize="@dimen/font_24"
                    android:textColor="@color/deep_gray"
                    android:background="@drawable/border"/>

            <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    android:orientation="horizontal">

                <TextView
                        android:layout_width="0dp"
                        android:layout_weight="1"
                        android:layout_height="match_parent"
                        android:gravity="center"
                        android:text="80~100"
                        android:textSize="@dimen/font_24"
                        android:textColor="@color/gray"
                        android:background="@drawable/border"/>

                <TextView
                        android:layout_width="0dp"
                        android:layout_weight="1"
                        android:layout_height="match_parent"
                        android:gravity="center"
                        android:text=">100"
                        android:textSize="@dimen/font_24"
                        android:textColor="@color/gray"
                        android:background="@drawable/border"/>


            </LinearLayout>

        </LinearLayout>

    </LinearLayout>

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

        <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:gravity="center"
                android:text="148"
                android:textSize="@dimen/font_24"
                android:textColor="@color/gray"
                android:id="@+id/textView8"
                android:background="@drawable/border"/>

        <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:gravity="center"
                android:text="5"
                android:textSize="@dimen/font_24"
                android:textColor="@color/gray"
                android:id="@+id/textView5"
                android:background="@drawable/border"/>

        <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:gravity="center"
                android:text="24"
                android:textSize="@dimen/font_24"
                android:textColor="@color/gray"
                android:background="@drawable/border"/>


        <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:orientation="horizontal">

            <TextView
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:text="3"
                    android:textSize="@dimen/font_24"
                    android:textColor="@color/gray"
                    android:id="@+id/textView6"
                    android:background="@drawable/border"/>

            <TextView
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:text="25"
                    android:textSize="@dimen/font_24"
                    android:textColor="@color/gray"
                    android:id="@+id/textView7"
                    android:background="@drawable/border"/>


        </LinearLayout>


    </LinearLayout>

</LinearLayout>

截图如下:
enter image description here

RecylerView 布局 XML:

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

    <include layout="@layout/view_line"/>

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:gravity="center">

        <TextView
                android:id="@+id/tv_start_date"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="2016-11-09"
                android:textSize="@dimen/font_24"/>

        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="  ~  "
                android:textSize="@dimen/font_24"/>

        <TextView
                android:id="@+id/tv_end_date"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="2016-11-09"
                android:textSize="@dimen/font_24"/>

    </LinearLayout>

    <include layout="@layout/view_line"/>

    <android.support.v7.widget.RecyclerView
            android:id="@+id/rclv_operative_table"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingTop="@dimen/screen_padding"
            android:paddingBottom="@dimen/screen_padding">

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

</LinearLayout>

最佳答案

我发现您的代码没有问题,所以我不确定我的更改是否有帮助,但请尝试一下。另外对于嵌套权重不利于性能,我想你知道它,所以你应该关心它。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:paddingTop="@dimen/screen_padding"
    android:paddingLeft="@dimen/screen_padding"
    android:paddingRight="@dimen/screen_padding">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="粤T24833"
            android:textSize="30sp"
            android:textColor="@color/deep_gray"
            android:background="@drawable/border"
            android:id="@+id/textView9"/>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="30dp"
                android:gravity="center"
                android:text="行驶里程(km): 10000"
                android:textSize="@dimen/font_24"
                android:textColor="@color/gray"
                android:background="@drawable/border"
                android:id="@+id/textView11"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="30dp"
                android:gravity="center"
                android:text="运行时间(min): 238"
                android:textSize="@dimen/font_24"
                android:textColor="@color/gray"
                android:background="@drawable/border"
                android:id="@+id/textView10"/>
        </LinearLayout>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="停车次数"
            android:id="@+id/textView"
            android:textSize="@dimen/font_24"
            android:textColor="@color/deep_gray"
            android:background="@drawable/border"/>

        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="熄火次数"
            android:id="@+id/textView2"
            android:textSize="@dimen/font_24"
            android:textColor="@color/deep_gray"
            android:background="@drawable/border"/>

        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="行驶次数"
            android:id="@+id/textView3"
            android:textSize="@dimen/font_24"
            android:textColor="@color/deep_gray"
            android:background="@drawable/border"/>


        <LinearLayout
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="20dp"
                android:gravity="center"
                android:text="提速次数"
                android:id="@+id/textView4"
                android:textSize="@dimen/font_24"
                android:textColor="@color/deep_gray"
                android:background="@drawable/border"/>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="20dp"
                android:orientation="horizontal">

                <TextView
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:text="80~100"
                    android:textSize="@dimen/font_24"
                    android:textColor="@color/gray"
                    android:background="@drawable/border"/>

                <TextView
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:text=">100"
                    android:textSize="@dimen/font_24"
                    android:textColor="@color/gray"
                    android:background="@drawable/border"/>
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="148"
            android:textSize="@dimen/font_24"
            android:textColor="@color/gray"
            android:id="@+id/textView8"
            android:background="@drawable/border"/>

        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="5"
            android:textSize="@dimen/font_24"
            android:textColor="@color/gray"
            android:id="@+id/textView5"
            android:background="@drawable/border"/>

        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="24"
            android:textSize="@dimen/font_24"
            android:textColor="@color/gray"
            android:background="@drawable/border"/>


        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="horizontal">

            <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:gravity="center"
                android:text="3"
                android:textSize="@dimen/font_24"
                android:textColor="@color/gray"
                android:id="@+id/textView6"
                android:background="@drawable/border"/>

            <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:gravity="center"
                android:text="25"
                android:textSize="@dimen/font_24"
                android:textColor="@color/gray"
                android:id="@+id/textView7"
                android:background="@drawable/border"/>
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

关于android - 布局内部的 LinearLayout 与父级不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40711879/

相关文章:

android - 如何在 Flatlist 中使用单选按钮 - React native

javascript - js2xmlparser 自定义 XML 正文

android - LinearLayout.getWidth() 不反射(reflect)来自 setLayoutParams 的新宽度

android - Android单元测试中如何通过ID访问资源?

version-control - 如何在颠覆中将应用程序的免费/付费版本分开

java - 从 xml 节点获取行号 - java

xml中的Android LinearLayout View项目不填充空白空间

android - 在 Android 模拟器上运行谷歌地图应用程序

android - Android 上的 SQLite 如何处理长字符串?

javascript - jQuery 查找对 xml 标记名使用通配符?