layout_weight解释

转自:

http://www.eoeandroid.com/forum.php?mod=viewthread&tid=75447&page=1


以前一直没弄懂Layout_weight是什么意思,自己写代码测试也出来了不同的情况,最近看了一篇帖子感觉分析的很好,转贴出来学习下。

布局文件是:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button1"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Button2"
/>
</LinearLayout>

出现的布局是:button1占了2/3,button2占了1/3。蓝色字体部分有解释。




但是如果将布局文件中的button的属性android:layout_width="fill_parent"改为android:layout_width="wrap_content"那么出现的结果为:button1占了1/3,button2占了2/3。请注意:其实并非是2:1的关系,实际上用button1.getWidth(),取得的结果并非如此。下面将会有解释,黄色背景的部分。



出现这样的结局是什么意思呢?下面是人家的详解:转载过来:

*******转载的解释*********

下面的解释是最强大之处:

linearLayout中包含有weight的child时,linearLayout会measure两次:
设屏幕宽度为X
第一次:button1的measuredWidth为X,button2也为X (因为用了weight,所以linearLayout每次measure child时不考虑前一个已经占用的大小),total_width为2X
第二次:计算delta=x-total_width=-x,然后会将button1的宽度设为
x+delta*1/3=0.66x, button2的宽度为 x+delta*2/3=0.33x

    原文中这部分的解释直接忽略(这里以灰色背景注明忽略部分,黄色背景部分为重点部分),本人以为以这篇

         http://hi.baidu.com/hbzha/blog/item/8af2b44f9bd8bd1eb2de055b.html

   为准:解释如下:

  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不同的组合得出来不同的结果!


*******转载的解释*********转载地址:http://hi.baidu.com/ljlkings/blog/item/fa2a59803f839a82f603a6b2.html?timeStamp=1305190390481



可以解释了:http://hi.baidu.com/hbzha/blog/item/8af2b44f9bd8bd1eb2de055b.html

                   红色颜色条彻底没了,对此不知道如何解释。????

设屏幕为x,则 totalWidth=4x. delta=x-total_width=-3x;

则红色条纹的宽度为:x+(2/5)(-3x)=(-1/5)x < 0;所以不会显示,同理green条纹为(2/5)x, blue条纹亦然,而此时只有(1/5)的空间了,由yellow条纹填充等。。。。。



本文整合了两篇博客的内容:

  1.  http://www.eoeandroid.com/forum.php?mod=viewthread&tid=75447&page=1

  2.  http://hi.baidu.com/hbzha/blog/item/8af2b44f9bd8bd1eb2de055b.html

有兴趣的请先查看此两篇。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值