天天看点

线性布局的权重weight使用详解

[size=large]对线性布局中权重的理解[/size]

转载请注明出处:[url]http://renyuan-1991.iteye.com/blog/2272200[/url]

今天突然想到自己对线性布局的理解,便想再此记录下来。写这篇博客之前特意看了很多别人写的线性布局的权重理解发现只有一篇是正确的,其余都是按“包裹内容时权重越大比例越大,匹配父窗体时权重越大比例越小”,或者反比正比的什么规律。这样的理解是不对的,对于自己写动态布局会产生很大的困扰,如果按这样的理解,下面的情况就无法解释了。

<LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_marginTop="50dp"
        android:layout_height="50dp">
        <TextView
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:background="@color/blue"
            android:layout_height="match_parent" />
        <TextView
            android:layout_weight="2"
            android:layout_width="match_parent"
            android:background="@color/red"
            android:layout_height="match_parent" />
        <TextView
            android:layout_weight="3"
            android:background="@color/yellow"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>
           

按照上面的说法这段代码显示的样式应该是3:2:1,可实际的情况是下图中的第二个线性布局

[img]http://dl2.iteye.com/upload/attachment/0114/6657/69544fb2-c011-3200-b0c6-f8f9cc097811.png[/img]

上面代码显示的布局中黄色的textview根本没有显示,所以“包裹内容时权重越大比例越大,匹配父窗体时权重越大比例越小”这种说法是不正确的。[color=orange]控件最终宽度 = 控件初始宽度+((屏幕宽度-控件宽度和)/(所有weight之和))[/color]*这里是水平方向的所以用宽度举例,竖直方向同理。

接下来我们看一下上图中对应的代码

<LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_marginTop="50dp"
        android:layout_height="50dp">
        <TextView
            android:layout_weight="1"
            android:layout_width="0dp"
            android:background="@color/blue"
            android:layout_height="match_parent" />
        <TextView
            android:layout_weight="2"
            android:layout_width="0dp"
            android:background="@color/red"
            android:layout_height="match_parent" />
        <TextView
            android:layout_weight="3"
            android:background="@color/yellow"
            android:layout_width="0dp"
            android:layout_height="match_parent" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_marginTop="50dp"
        android:layout_height="50dp">
        <TextView
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:background="@color/blue"
            android:layout_height="match_parent" />
        <TextView
            android:layout_weight="2"
            android:layout_width="match_parent"
            android:background="@color/red"
            android:layout_height="match_parent" />
        <TextView
            android:layout_weight="3"
            android:background="@color/yellow"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_marginTop="50dp"
        android:layout_height="50dp">
        <TextView
            android:layout_weight="1"
            android:layout_width="0dp"
            android:background="@color/blue"
            android:layout_height="match_parent" />
        <TextView
            android:layout_weight="1"
            android:layout_width="0dp"
            android:background="@color/red"
            android:layout_height="match_parent" />
        <TextView
            android:layout_weight="1"
            android:background="@color/yellow"
            android:layout_width="100dp"
            android:layout_height="match_parent" />
    </LinearLayout>
           

第一段代码:控件宽度之和是0dp,所以屏幕剩余量是1个屏幕宽度。weight之和是6。先把屏幕剩余量分6份,第一个textview占一份第二个TextView占两份第三个TextView占三份。最后它们显示的宽度就是0+1/6,0+2/6,0+3/6。

第二段代码:控件宽度之和是3个屏幕宽度,所以屏幕剩余量是-2个屏幕宽度。weight之和是6。先把屏幕剩余量分6份,第一个textview占一份第二个TextView占两份第三个TextView占三份。最后它们显示的宽度就是1+(1/6)*(-2),1+(2/6)*(-2),1+(3/6)*(-2)。所以第一个TextView和第二个TextView已经占满了屏幕第三个TextView自然就显示不出来了

第三段代码:控件宽度之和是100dp,所以屏幕剩余量是1个屏幕宽度减去100dp。weight之和是6。先把屏幕剩余量分6份,第一个textview占一份第二个TextView占两份第三个TextView占三份。最后它们显示的宽度就是0+(1/6)*剩余量,0+(2/6)*剩余量,100+(3/6)*剩余量

ok,到这儿就结束了....

希望爱好编程的小伙伴能加这个群,互相帮助,共同学习。群号: 141877583

继续阅读