Layout_weight详解

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="horizontal" >  
  6.   
  7.     <TextView  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:background="#0045f5"  
  11.         android:gravity="center"  
  12.         android:text="1" />  
  13.   
  14.     <TextView  
  15.         android:layout_width="wrap_content"  
  16.         android:layout_height="wrap_content"  
  17.         android:background="#00ff47"  
  18.         android:gravity="center"  
  19.         android:text="2"   
  20.         android:layout_weight="1"/>  
  21.   
  22.     <TextView  
  23.         android:layout_width="wrap_content"  
  24.         android:layout_height="wrap_content"  
  25.         android:background="#ff5600"  
  26.         android:gravity="center"  
  27.         android:layout_weight="1"  
  28.         android:text="3" />  
  29.   
  30. </LinearLayout>  


为什么效果是这个样子呢,首先3个文本框的宽度都是“wrap_content”,根据视图内部内容自动扩展,LinearLayout就先给3个TextView分配空间适当的空间大小,假设为每个TextView分配10dip的宽度,屏幕的宽度为480dip, 那么LinearLayout的剩余空间就是 480 - 3*10 = 450dip,由于第一个TextView没有设置layout_weight,所以它的宽度就是10dip,而后面两个TextView设置layout_weight都是1,所以后面两个TextView就平均分配LinearLayout的剩余空间,即为 450 / 2  = 225dip,所以后面两个TextView的宽度为10 + 225 = 235dip

2.

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="horizontal" >  
  6.   
  7.     <TextView  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:background="#0045f5"  
  11.         android:gravity="center"  
  12.         android:text="1" />  
  13.   
  14.     <TextView  
  15.         android:layout_width="wrap_content"  
  16.         android:layout_height="wrap_content"  
  17.         android:background="#00ff47"  
  18.         android:gravity="center"  
  19.         android:text="2222222222222222222"   
  20.         android:layout_weight="1"/>  
  21.   
  22.     <TextView  
  23.         android:layout_width="wrap_content"  
  24.         android:layout_height="wrap_content"  
  25.         android:background="#ff5600"  
  26.         android:gravity="center"  
  27.         android:layout_weight="1"  
  28.         android:text="3" />  
  29. </LinearLayout>  

你本来想让后面两个TextView平均分配剩余控件,可是下面的效果却并不是你想要的,如下图



其实因为3个TextView的宽度都是”wrap_content“,LinearLayout会先按照TextView里面的内容分配好大小,由于第2个TextView内容很多,所以LinearLayout为其分配更多的空间,使得剩余空间变小了,原理和上面的一样,那么我们在实际开发中要怎么设置按比例分配呢。知道原理其实就很简单,比如我们想要3个TextView按照1:2:3的效果

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="horizontal" >  
  6.   
  7.     <TextView  
  8.         android:layout_width="0dip"  
  9.         android:layout_height="wrap_content"  
  10.         android:background="#0045f5"  
  11.         android:gravity="center"  
  12.         android:layout_weight="1"  
  13.         android:text="1" />  
  14.   
  15.     <TextView  
  16.         android:layout_width="0dip"  
  17.         android:layout_height="wrap_content"  
  18.         android:background="#00ff47"  
  19.         android:gravity="center"  
  20.         android:text="2222222222222222222"   
  21.         android:layout_weight="2"/>  
  22.   
  23.     <TextView  
  24.         android:layout_width="0dip"  
  25.         android:layout_height="wrap_content"  
  26.         android:background="#ff5600"  
  27.         android:gravity="center"  
  28.         android:layout_weight="3"  
  29.         android:text="3" />  
  30. </LinearLayout>  


我们只需要将3个TextView的宽度设置为0dip,首先LinearLayout为3个TextView分配0dip的宽度,剩余空间就是 480 - 3 * 0 = 480dip,然后剩余空间在按照权重分配,所以我们看到的效果就是1:2:3


通过上面的讲解,也许你会得出一个结论,权重越大,LinearLayout为其分配的空间就越大,我只能说这个结论下有点早了,我们继续看布局

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="horizontal" >  
  6.   
  7.     <TextView  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:background="#0045f5"  
  11.         android:gravity="center"  
  12.         android:layout_weight="1"  
  13.         android:text="1" />  
  14.   
  15.     <TextView  
  16.         android:layout_width="fill_parent"  
  17.         android:layout_height="wrap_content"  
  18.         android:background="#00ff47"  
  19.         android:gravity="center"  
  20.         android:text="2"   
  21.         android:layout_weight="2"/>  
  22.   
  23.     <TextView  
  24.         android:layout_width="fill_parent"  
  25.         android:layout_height="wrap_content"  
  26.         android:background="#ff5600"  
  27.         android:gravity="center"  
  28.         android:layout_weight="2"  
  29.         android:text="3" />  
  30. </LinearLayout>  


也许你会很纳闷,怎么不是你想要的1:2:2的效果,我来为你解决疑惑吧,原理跟上面的还是一样的,因为我们这里为每个TextView设置的宽度为”fill_parent",即为充满整个LinearLayout,假如屏幕依然为480dip, 首先LinearLayout为3个TextView分配的宽度为480dip,屏幕剩余宽度为 480 - 3* 480 = -960dip,然后3个TextView按照权重分配剩余空间,第一个TextView分配宽度为 480 + (-960) * (1/5) = 288dip,后面两个TextView就为480 + (-960) * (2/5) = 96dip,比例为3:1:1

参考网址:http://blog.csdn.net/xiaanming/article/details/13630837

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值