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

时间:2016-11-21 02:23:11

标签: android xml android-layout

当方向为水平,宽度为0dp且设置权重时,

LinearLayout内部布局不匹配父级。此XML文件用于RecyclerView项目布局。布局XML文件如下:

import java.util.*;
public class qsort {
    public static void main(String []args)
    {
        int []a1 = { 1, 2, 3, -4, -5, 6, 7 };
        sort( 0, a1.length-1, a1 );
        printarray (a1 );

    }
static void sort( int low, int high, int []a )
{
    if ( low < high ) {
        int p = part( low, high, a);
        System.out.println( " p = " + p );
        printarray( a, low, p );
        System.out.println();
        sort( low, p-1, a);
        printarray( a, p, high );
        System.out.println();
        sort( p+1, high, a);
    }
}
static int part(int low, int high, int [] S) {
        int i;
        int j;
        int randspot;
        int pivotitem;
        int pivotpoint;
        Random random = new Random(456);
        randspot = random.nextInt(high-low +1)+low;
        pivotitem = S[randspot];
        j = low;
        for (i = low + 1; i <= high; i++)
            if (S[i] < pivotitem){
                j++;
                int temp = S[j];
                S[j] = S[i]; //exchanges S[i] and S[j]
                S[i] = temp;
            }
        pivotpoint = j;
        int temp = S[low];
        S[low] = S[pivotpoint];
        S[pivotpoint] = temp;
        return pivotpoint;
    }

static void printarray (int [] Test){
    for (int i = 0; i<Test.length; i++){
        if (i !=0 && i%10==0)
            System.out.println();
        System.out.print(Test[i]+ "\t");

    }
}
static void printarray (int [] Test, int low, int high){
    for (int i = low; i<high; i++)
        System.out.print(Test[i]+ "\t");
    }
}

屏幕截图如下:
enter image description here

RecylerView Layout 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>

1 个答案:

答案 0 :(得分:1)

我发现您的代码没有问题,所以我不确定我的更改可以提供帮助,但请尝试一下。同样对于Nested weights are bad for performance,我想你知道,所以你应该关心它。

<?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>