`
zyoo005
  • 浏览: 18564 次
  • 性别: Icon_minigender_1
  • 来自: 内蒙古
社区版块
存档分类
最新评论

Android的layout_weight属性释疑

阅读更多

 

Android的layout_weight属性释疑
转载

 

   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.

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics