android--LinearLayout的child中layout_weight的作用与使用

linearLayout中包含有weight的child时,linearLayout会measure两次:

第一次 测量 child 的 原始值:

第二次 测量 child 的 比重值:

然后将2次测量的值相加,得到child 的具体的宽 或 高。

//-----------------------------------------------------------------------------

我们假设 一个LinearLayout方向为横向android:orientation="horizontal" ,它有2个child,分别为 child_1 和 child_2。

我们来分析下 这2个child的宽度在带有layout_weight属性的情况下,值是怎么计算的。

第一次测量时:有自己的layout_width参数决定

首先,child的宽度(layout_width)有3种情况:0dp(具体值,可以随意设置,例如 100dp)、wrap_content、match_parent。

这3种情况,当应用加载到具体的手机上时,都会在测量时,变为具体的值,分别如下:

1,当child_1 的 layout_width 为 0dp,它的width_orginal 原始宽度为0pix;(100dp,则 width_orginal 为 屏幕密度density * 100 pix = X_1 pix)

  (其中手机屏幕密度 通过 context.getResources().getDisplayMetrics().density 获取)

2,当child_1 的 layout_width 为 wrap_content,它的width_orginal 原始宽度为 int_wrap_cotent(在具体的手机上是一个固定值,假设为 X_1  pix) ;

3,当child_1 的 layout_width 为 match_parent,它的width_orginal 原始宽度为手机屏幕的宽度(在具体的手机上是一个固定值,假设为 X_1 pix) ;

(child_2 的 原始宽度为  X_2)

第二次测量时:由所有的child 的 layout_weight 参数共同决定

假设 2个child的 layout_weight 分别为 weight_1 和 weight_2 (layout_weight 参数 是一个int类型值,可以为0):

那么 child_1 和 child_2 的最终宽度为:

child_1的宽度:X_1 + (手机屏幕宽度 - (X_1 + X_2))  * weight_1 / (weight_1+weight_2) = 最终宽度。

以下是几个例子:

demo1:child_1 和 child_2 的layout_width都为0dp,layout_weight分别为 1 和 2

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:id="@+id/root"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:orientation="horizontal" >
 7 
 8     <TextView
 9         android:id="@+id/tv_left"
10         android:layout_width="0dp"
11         android:layout_height="wrap_content"
12         android:layout_weight="1"
13         android:background="#00ff00"
15         android:text="child_1"
16         android:textSize="24sp" />
17 
18     <TextView
19         android:id="@+id/tv_right"
20         android:layout_width="0dp"
21         android:layout_height="wrap_content"
22         android:layout_weight="2"
23         android:background="#ff0000"
24 
25         android:text="child_2"
26         android:textSize="24sp" />
27 
28 </LinearLayout>

此时,child_1 的最终宽度为:0 pix + 屏幕宽度 * 1/3 = 1/3 的屏幕宽度。child_2 最终宽度为 2/3 的屏幕宽度。

所以想要按比例分配LinearLayout的children的宽(高)值,可以用次方法。

demo2:child_1 和 child_2 的layout_width都为100dp,layout_weight分别为 1 和 2。

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:id="@+id/root"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:orientation="horizontal" >
 7 
 8     <TextView
 9         android:id="@+id/tv_left"
10         android:layout_width="100dp"
11         android:layout_height="wrap_content"
12         android:layout_weight="1"
13         android:background="#00ff00"
14 
15         android:text="child_1"
16         android:textSize="24sp" />
17 
18     <TextView
19         android:id="@+id/tv_right"
20         android:layout_width="100dp"
21         android:layout_height="wrap_content"
22         android:layout_weight="2"
23         android:background="#ff0000"
24 
25         android:text="child_2"
26         android:textSize="24sp" />
27 
28 </LinearLayout>

此时,child_1 的最终宽度为:100 * density pix + (屏幕宽度 - 200 dp * density) * 1/3 = 。。。

demo2:child_1 和 child_2 的layout_width都为match_parent,layout_weight分别为 1 和 2。

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:id="@+id/root"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:orientation="horizontal" >
 7 
 8     <TextView
 9         android:id="@+id/tv_left"
10         android:layout_width="match_parent"
11         android:layout_height="wrap_content"
12         android:layout_weight="1"
13         android:background="#00ff00"
14 
15         android:text="child_1"
16         android:textSize="24sp" />
17 
18     <TextView
19         android:id="@+id/tv_right"
20         android:layout_width="match_parent"
21         android:layout_height="wrap_content"
22         android:layout_weight="2"
23         android:background="#ff0000"
24 
25         android:text="child_2"
26         android:textSize="24sp" />
27 
28 </LinearLayout>

此时,child_1 的最终宽度为:屏幕宽度 + (屏幕宽度 - 2倍的屏幕宽度) * 1/3 = 2/3 的 屏幕宽度,child_2为 1/3 的屏幕宽度。

当 child 的 layout_width 都为 match_parent时,想要 child_1 : child_2 = 1 : 2(即 1/3 : 2/3),可以设置 child_1 child_2 的 layout_weight 为 (分母 - 1) : (分母 - 2).

当 child 的 layout_width 都为 match_parent时,想要child_1 : child_2 : child_3 = 3 : 4 : 5 (即 3/12 : 4/12: 5/12),可以设置layout_weight 分别为 (12-3):(12-4):(12-5)

 

posted @ 2014-12-25 17:47  王自强  阅读(435)  评论(0编辑  收藏  举报