Android的layout_weight属性详解

来源:互联网 发布:手机电缆报价软件 编辑:程序博客网 时间:2024/05/16 05:49

layout_weight是LinearLayout布局里一个重要的属性,就像Qt里的stretch一样,把父视图剩余的空间分配给设置了layout_weight的组件。这个属性可以让LinearLayout里不同的组件分配不同宽度/高度变得非常灵活。Android官网里对layout_weight如下解释:

LinearLayout also supports assigning a weight to individual children. This attribute assigns an "importance" value to a view, and allows it to expand to fill any remaining space in the parent view. Child views can specify an integer weight value, and then any remaining space in the view group is assigned to children in the proportion of their declared weight. Default weight is zero. For example, if there are three text boxes and two of them declare a weight of 1, while the other is given no weight (0), the third text box without weight will not grow and will only occupy the area required by its content. The other two will expand equally to fill the space remaining after all three boxes are measured. If the third box is then given a weight of 2 (instead of 0), then it is now declared "more important" than both the others, so it gets half the total remaining space, while the first two share the rest equally.

    大意就是layout_weight的值越大,所占比例也越大。没错,这跟我们平常理解的stretch是一致的,但是如果把它理解成layout_weight值相同则组件占用宽度或高度一致的话就错了,这里说的是布局完了之后的剩余空间如何分配,layout_weight相同只说明剩余空间的分配大小相同,而组件的实际宽度/高度则是组件需要的空间加上layout_weight分配的空间。layout_weight的设置和layout_height,layout_width不同的组合得出来不同的结果!

    且先看官网上关于LinearLayout的例子:

<?xml version="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent">

 <LinearLayout
     android:orientation="horizontal"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:layout_weight="1">
     <TextView
         android:text="red"
         android:gravity="center_horizontal"
         android:background="#aa0000"
         android:layout_width="wrap_content"
         android:layout_height="fill_parent"
         android:layout_weight="1"/>
     <TextView
         android:text="green"
         android:gravity="center_horizontal"
         android:background="#00aa00"
         android:layout_width="wrap_content"
         android:layout_height="fill_parent"
         android:layout_weight="1"/>
     <TextView
         android:text="blue"
         android:gravity="center_horizontal"
         android:background="#0000aa"
         android:layout_width="wrap_content"
         android:layout_height="fill_parent"
         android:layout_weight="1"/>
     <TextView
         android:text="yellow"
         android:gravity="center_horizontal"
         android:background="#aaaa00"
         android:layout_width="wrap_content"
         android:layout_height="fill_parent"
         android:layout_weight="1"/>
 </LinearLayout>
        
 <LinearLayout
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:layout_weight="1">
   <TextView
       android:text="row one"
       android:textSize="15pt"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:layout_weight="1"/>
   <TextView
       android:text="row two"
       android:textSize="15pt"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:layout_weight="1"/>
   <TextView
       android:text="row three"
       android:textSize="15pt"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:layout_weight="1"/>
   <TextView
       android:text="row four"
       android:textSize="15pt"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:layout_weight="1"/>
 </LinearLayout>
</LinearLayout>

  在2.2的模拟器上显示的结果如下:

 

 

各颜色条的宽度是不一致的,因为red,green,blue和yellow这几个字符串的长度不一样,如果将几个标签都改成test,则得到:

 

 

现在四个颜色条的宽度是相同的了,如果想既保持字符串不一致,又想得到相同宽度的颜色条:

把TextView中的属性android:layout_width值改为"fill_parent"

得到的结果似乎可以满足要求:

 

 

 

 

再这个基础上red颜色条的layout_weight设置为2。

 

 

 

红色颜色条彻底没了,对此不知道如何解释。如果layout_width是wrap_content的话则是正常的:

 

注意,这里几个颜色条的占用比例并不是2:1:1:1,原因之前说了。

利用嵌套的LinearLayout可以达到2:1:1:1的显示效果

<LinearLayout android:orientation="horizontal"
 android:layout_width="fill_parent" android:layout_height="fill_parent"
 android:layout_weight="1">

 <LinearLayout android:orientation="horizontal"
  android:layout_width="fill_parent" android:layout_height="fill_parent"
  android:layout_weight="3">
  <TextView android:text="red" android:gravity="center_horizontal"
   android:background="#aa0000" android:layout_width="fill_parent"
   android:layout_height="fill_parent" />
 </LinearLayout>
 <LinearLayout android:orientation="horizontal"
  android:layout_width="fill_parent" android:layout_height="fill_parent"
  android:layout_weight="2">
  <LinearLayout android:orientation="horizontal"
   android:layout_width="fill_parent" android:layout_height="fill_parent"
   android:layout_weight="1">
   <TextView android:text="green" android:gravity="center_horizontal"
    android:background="#00aa00" android:layout_width="fill_parent"
    android:layout_height="fill_parent" />
  </LinearLayout>
  <LinearLayout android:orientation="horizontal"
   android:layout_width="fill_parent" android:layout_height="fill_parent"
   android:layout_weight="1">
   <TextView android:text="blue" android:gravity="center_horizontal"
    android:background="#0000aa" android:layout_width="fill_parent"
    android:layout_height="fill_parent" />
  </LinearLayout>
  <LinearLayout android:orientation="horizontal"
   android:layout_width="fill_parent" android:layout_height="fill_parent"
   android:layout_weight="1">
   <TextView android:text="yellow" android:gravity="center_horizontal"
    android:background="#aaaa00" android:layout_width="fill_parent"
    android:layout_height="fill_parent" />
  </LinearLayout>
 </LinearLayout>
</LinearLayout>

 

再测试LinearLayout之间的layout_weight设置带来的影响,将第一个LinearLayout的layout_weight设置为2之后:



和view里的效果刚好相反,layout_weight大的LinearLayout占得比例最小,刚好成反比。不知道Google为什么要采用这样截然相反的策略。

 

最后总结一下吧:

1. LinearLayout内部的子控件之间的layout_weight是按照正比例分配空间

2. LinearLayout之间的layout_weight是按照反比例分配空间

3. 在Horizontal的LinearLayout中,控件A和控件B的layout_weight分别设置为2和1,并不代表两者的宽度之比为2:1。控件的宽度等于空间本身需要的宽度,加上通过layout_weight设置分配到了父空间里的宽度。垂直方向的LinearLayout也同理。

4.要想实现控件A和控件B的宽度严格按比例显示,可以每个控件之上都添加一个LinearLayout,在LinearLayout的属性里设置layout_weight.

 

 

目前网上关于Android layout_weight属性的解释大多观点为:

 

1. LinearLayout内部的子控件之间的layout_weight是按照正比例分配空间
2. LinearLayout之间的layout_weight是按照反比例分配空间
3. 在Horizontal的LinearLayout中,控件A和控件B的layout_weight分别设置为2和1,并不代表两者的宽度之比为2:1。 控件的宽度等于空间本身需要的宽度,加上通过layout_weight设置分配到了父空间里的宽度。垂直方向的LinearLayout也同理。
4.要想实现控件A和控件B的宽度严格按比例显示,可以每个控件之上都添加一个LinearLayout,在LinearLayout的属性里设置layout_weight.

在layout_width設置為fill_parent的時候,layout_weight所代表的是你的控件要優先盡可能的大,但這個大是有限度的,即fill_parent.
在layout_width設置為wrap_content的時候,layout_weight所代表的是你的控件要優先盡可能的小,但這個小是有限度的,即wrap_content.
layout_height 同 layout_width.

 

其实,上述观点并没有完全涵盖了layout_weight的所有情况,例如LinearLayout下的子控件既有fill_parent又有wrap_content的情况下如何分配空间。经试验,layout_weight属性的作用可以总结如下:

 

假设在一个Layout下的子控件有C1、C2、…、Cn,layout_weight值为W1、W2、…、Wn,总layout_weight值为W=W1+W2+…+Wn。

先不考虑每个子控件的layout_weight值和子控件间的相互影响,对于每个子控件,按照Layout下只有该控件单独存在的情况来确定它的大小,结果为S1、S2、…、Sn,总长度为S=S1+S2+…+Sn。

假设layout的大小计算出来最大为L。

那么:

(1)如果L>=S,则各个子控件的最终大小为S1、S2、…、Sn。

(2)如果L<S,则计算差值为d=S-L,此差值由各控件按照layout_weight值的比例来承担。则各个子控件的最终大小为