关于对Layout_weight的理解

    最近一段时间,有朋友常常问我关于android布局文件里Layout_weight究竟是什么做用,以前我也被困扰,随着知识的增加,也慢慢知道了原因,并将本身的理解分享以下: android

    layout_weight的权重不是指父控件的权重,而是指父控件的空间按子控件大小分配完后剩余空间的权重。 布局

    直接上代码上图了 spa

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#800FF8"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@android:color/black"
            android:text="weight=1"
            android:textColor="@android:color/white" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:background="@android:color/white"
            android:text="weight=2"
            android:textColor="@android:color/black" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:background="@android:color/black"
            android:text="weight=3"
            android:textColor="@android:color/white" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="4"
            android:background="@android:color/white"
            android:text="weight=4"
            android:textColor="@android:color/black" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="3dp"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@android:color/black"
            android:text="weight=1"
            android:textColor="@android:color/white" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:background="@android:color/white"
            android:text="weight=2"
            android:textColor="@android:color/black" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:background="@android:color/black"
            android:text="weight=3"
            android:textColor="@android:color/white" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="4"
            android:background="@android:color/white"
            android:text="weight=4"
            android:textColor="@android:color/black" />
    </LinearLayout>

</LinearLayout> 3d

运行结果: xml

    第二行视图中weight=2超出屏幕,weight=3跑偏了,weight=4没了。 开发

    其实android是这样计算的(以布局文件中第一行视图为例): it

    首先,android会给每一个TextView分配wrap_content宽度,当4个TextView宽度加起来小于屏幕总宽度的话,那一切都会按咱们想象的出现4个TextView逐渐增大;反之大于屏幕宽度则会出现布局错位(能够经过如下方法计算错位的位置); io

    而后,android将屏幕宽度减去4个TextView总宽度后获得余量(有可能为负),再根据每一个TextView的weight分配余量; 方法

    最后,将TextView的宽度加上对应从余量中所分配的大小,就是实际要显示的宽度。 im

    第二行视图也可经过一样的计算所得,发现Weight=2超过屏幕大小,weight=3跑偏了,weight=4计算为负,显示不出来。

    因此,开发过程当中,建议子控件用match_parent,而且为每一个子控件分配一样的权重,每一个子控件所包含的内容大小尽可能一致。

相关文章
相关标签/搜索