文中对layout_weight的使用环境进行了描述,我总结一下,这是我的测试条件:
1,控件所在layout必须是LinearLayout。
2,LinearLayout的orientation属性设置成“horizontal”。
3,子控件的layout_width属性设置为“fill_parent”。
1,两个子控件,1:1
xml:
1 < LinearLayout android:id ="@+id/custom_titlebar" 2 xmlns:android ="http://schemas.android.com/apk/res/android" 3 android:orientation ="horizontal" 4 android:layout_width ="fill_parent" 5 android:layout_height ="wrap_content" > 6 7 < Button android:id ="@+id/imageViewLoginState" 8 android:layout_width ="fill_parent" 9 android:layout_height ="fill_parent" 10 android:layout_weight ="1" ></ Button > 11 12 < Button android:id ="@+id/imageViewLoginState1" 13 android:layout_width ="fill_parent" 14 android:layout_height ="fill_parent" 15 android:layout_weight ="1" ></ Button > 16 </ LinearLayout >
效果很明显,1:1
2,两个子控件,1:2
xml:
< LinearLayout android:id ="@+id/custom_titlebar" xmlns:android ="http://schemas.android.com/apk/res/android" android:orientation ="horizontal" android:layout_width ="fill_parent" android:layout_height ="wrap_content" > < Button android:id ="@+id/imageViewLoginState" android:layout_width ="fill_parent" android:layout_height ="fill_parent" android:layout_weight ="1" ></ Button > < Button android:id ="@+id/imageViewLoginState1" android:layout_width ="fill_parent" android:layout_height ="fill_parent" android:layout_weight ="2" ></ Button > </ LinearLayout >
效果很明显,2:1
3,三个子控件,1:1:1
xml不再贴了,效果很明显,1:1:1
4,三个子控件,1:1:2(好戏来了)
xml:
< LinearLayout android:id ="@+id/custom_titlebar" xmlns:android ="http://schemas.android.com/apk/res/android" android:orientation ="horizontal" android:layout_width ="fill_parent" android:layout_height ="wrap_content" > < Button android:id ="@+id/imageViewLoginState" android:layout_width ="fill_parent" android:layout_height ="fill_parent" android:text ="button1" android:layout_weight ="1" ></ Button > < Button android:id ="@+id/imageViewLoginState1" android:layout_width ="fill_parent" android:layout_height ="fill_parent" android:text ="button2" android:layout_weight ="1" ></ Button > < Button android:id ="@+id/imageViewLoginState2" android:layout_width ="fill_parent" android:layout_height ="fill_parent" android:text ="button3" android:layout_weight ="2" ></ Button > </ LinearLayout >
效果很奇怪,你能猜到吗:
怪处有二:1,button3消失了。2,button1、button2变成了胖子(经测量,高度是原来的两倍)。
4,三个子控件,2:1:2
xml不再贴了,效果依然很怪异:
实在不好意思,能力有限,贴到这里,我还没有总结出规律,先到此为止吧,您有什么好建议,可以发给我
网上的回复:
weight在此不能按比例关系进行理解。
weight的意思是重量、分量的意思,按这个翻译的意思我们可以理解为权重、重要程度。weight值越大重要性越大。
当VIEW的weight存在差异时,会先满足不重要的VIEW显示,不重要的VIEW占用后的剩余空间会全部给重要性高的VIEW显示,这个剩余空间可能会较大,也可能较小,甚至肉眼看不到,但不代表不存在。
你的最后一个VIEW没有显示的原因:就是前两个BUTTON已经占满了横屏,剩下的空间很小,分配了BUTTON3,肉眼是看不到的。被拉高的原因就是BUTTON3的text要显示,但剩余空间不足横屏的显示,因此需要垂直显示。