android学习-组件中的layout-weight属性

早上摆弄布局的时候注意到了这个,

lanyout_weight:用于给一个线性布局中的诸多视图的重要度赋值,默认为零,数值越小,重要度越高,先来看一下效果:

[img]http://dl.iteye.com/upload/attachment/289220/08f88d94-00ad-3d91-855c-a0faf62d3e66.png[/img]

这里有两个Linearlayout,上面一个是三张图片,下面是几句话,这里分别给两个
Linearlayout设置lanyout_weight属性为1,2;在第一个Linearlayout中分别给三张图片设置设置lanyout_weight属性为1,2,3;

然后看另一个效果:

[img]http://dl.iteye.com/upload/attachment/289222/74e938c1-dda2-3ed4-82c5-5ed6c305deda.png[/img]


这里的变化只是把三张图片的lanyout_weight属性设置为1,1,3

现在再看lanyout_weight属性都为1时的布局:

[img]http://dl.iteye.com/upload/attachment/289225/ac192cb9-8628-3984-9920-fb290d0ca3cc.png[/img]


三张图对比来看会给人一种错觉即lanyout_weight属性是某个组件在布局中所占的比例,
我一开始也是这么想的,但是后面发现2个问题:
1.三张图都设置为1时他们并没有平分,第三个图的比例大于前两图
2.Linearlayout本身的lanyout_weight属性和Linearlayout中组件的lanyout_weight属性的效果是反过来的(Linearlayout中lanyout_weight低的所占比例大,TextView中lanyout_weight高的所占比例大)

百度了一下有人说 “在一个LinearLayout内各个组件是按weight值以正比形式显示在屏幕上 而在LinearLayout之间以反比的形式显示在屏幕上 ”。。。

我想这个属性不会这么二吧不同组件的效果还不一样。。

最后搞到官方文档关于这个属性的解释
[quote]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.[/quote]

其实很简单,weight本来的意思就是重量,即[color=red]这个属性所代表的是重要程度,而不是比例[/color]

如果三个view横列,前两个view的wieght是1,第三个是默认值0。可见第三个view的权weight最低,那么第三个view就只会占用自己内容所需的宽度,剩下的宽度会被前两个view平分。

如果第三个view的wieght是2,那么前两个view就只占据自己内容所需的宽度,第三个会占据剩下所有空间。

至于Linearlayout的weight属性越小的所占空间越多,我觉得这是因为Linearlayout的默认高度问题,一个简单的实验当我把两个Linearlayout的值设为1和5的时候,屏幕比例依然不变,因为第一个Linearlayout的默认大小就是那么大
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值