Android开发拾穗Android知识点程序员

关于layout_weight,你到底知多少

2016-05-11  本文已影响1686人  a57ecf3aaaf2

layout_weight 是Android线性布局中的权重表示方式,一定程度上用来表示子布局所占父布局的比重。

若C-child表示子布局声明的大小,B-blank表示剩余布局的大小,P-percent表示子布局占据父布局剩余布局的比例,则子布局最终的实际大小R-reality为:

R = C + B * P

通过以上公式,下面的布局依然成立:
left所占宽度:R = 0 + B * (1/2) = (1/2) * B

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

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/orange_ff9800"
        android:padding="6dp"
        android:text="left"
        android:textColor="@color/white"/>

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/blue_31b6e7"
        android:padding="6dp"
        android:text="right"
        android:textColor="@color/white"/>

</LinearLayout>
layout_weight

LinearLayout有个权重数量的标记:weightSum,以上布局中两个TextView分别声明了自己的权重,在LinearLayout没有声明 weightSum 时,默认的权重数量就是子布局权重之和:2。

如果将以上布局的Linearlayou权重之和声明为4:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="4"
    android:background="@color/gray_e0e0e0">

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/orange_ff9800"
        android:padding="6dp"
        android:text="left"
        android:textColor="@color/white"/>

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/blue_31b6e7"
        android:padding="6dp"
        android:text="right"
        android:textColor="@color/white"/>

</LinearLayout>

则子布局的显示如下:

每个TextView占据1/4

谷歌官方建议子布局的layout_width使用0dp,来分比例显示布局,和wrap_content大同小异,当使用layout_weight时,都表示占据剩余宽度或高度的比重。

但两者有明显区别。

使用0dp时,要考虑所分配的布局宽度是否小于控件实际宽度,比如:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:background="@color/gray_e0e0e0">

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/orange_ff9800"
        android:padding="6dp"
        android:text="left"
        android:textColor="@color/white"/>

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="19"
        android:background="@color/blue_31b6e7"
        android:padding="6dp"
        android:text="right"
        android:textColor="@color/white"/>

</LinearLayout>
left布局变窄了

而使用wrap_content不必考虑这样的问题:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:background="@color/gray_e0e0e0">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/orange_ff9800"
        android:padding="6dp"
        android:text="left"
        android:textColor="@color/white"/>

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="19"
        android:background="@color/blue_31b6e7"
        android:padding="6dp"
        android:text="right"
        android:textColor="@color/white"/>

</LinearLayout>
left适应自身布局后再获取剩余布局的1/20

当子布局的高宽声明为match_parent时,所显示的效果与wrap_content 截然相反。

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:background="@color/gray_e0e0e0">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/orange_ff9800"
        android:padding="6dp"
        android:text="left"
        android:textColor="@color/white"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:background="@color/blue_31b6e7"
        android:padding="6dp"
        android:text="right"
        android:textColor="@color/white"/>

</LinearLayout>
left和right所声明的layout_weight与实际显示截然相反

究其原因,都是match_parent在捣鬼。

来看下文章开始时的公式:
R = C + B * P

以left控件为例,此时的B(剩余布局) = 父布局大小 - 子控件大小之和,父布局大小也就是C(因为声明的是match_parent),而子控件大小之和是2C,所以 :

B = C - 2C = -C ,
R = C + B * P = C + (-C) * (1/3) = (2/3) * C

其实,以上公式恒成立,关键在于,当子控件的高宽声明为match_parent时的剩余布局大小要搞清楚。剩余布局大小 = 父布局大小 - 子布局大小之和

上一篇 下一篇

猜你喜欢

热点阅读