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

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


2.layout_weight只负责把父视图剩余的空间按指定比例分配,所以如果在一个LinearLayout的子控件有的有该属性,有的没有的话。所有控件按自己指定宽高分配完毕后,父视图还有空间,那些有layout_weight的控件按自己所占比重分配剩余空间,比重值可以使任意值,比如LinearLayout空间的子控件比重分别为1,3,6,7,8。比重为三的空间分配剩余空间大小为3/(1+3+6+7+8)=3/25(此原则适用于宽高两方面).


3.要想实现控件A和控件B的宽度严格按比例显示,可使子控件宽或高(是布局方向而定)为0dp,此时按正比显示(最常用,因为是正比例分配,符合正常逻辑);

使子控件宽或高(是布局方向而定)为fill_pararent,此时按反比显示;


4,该属性使用与LinearLayout和tablerow中,目前只知道这两种。必须还有很多使用地方,大概认为凡是依次按顺序排列的viewgrow都适用此属性。


原文摘自http://hi.baidu.com/mendynew/item/39cd374192770bab60d7b915 根据自己不同理解总结部分有更正

更多相关文章

  1. ReactNative的ViewPagerAndroid简述
  2. Android(安卓)开发笔记4-- 常用控件
  3. Android(安卓)Chronometer控件使用,计时器
  4. Android(安卓)M 新控件 TabLayout 与 NavigationView 实践
  5. Android布局和intent实例
  6. Android处理touch冲突的解决办法
  7. Android学习笔记十四之RelativeLayout相对布局
  8. Android(安卓)自定义控件实现ListView索引
  9. (笔记)Android(安卓)studio——相对布局(RelativeLayout)

随机推荐

  1. android基础控件(4)GridView实现网格视图
  2. Android(安卓)自动完成文本框的实例
  3. android studio 实现再按一次返回键退出
  4. Android(安卓)音效提示与振动提示
  5. Android联网方式判断
  6. android短信监听
  7. android 三种解析,构建xml方法
  8. Android(安卓)线程
  9. 【Android】如何寻找出某个Intent是否可
  10. Android中如何实现EditText的自动换行