对android LinearLayout中layout_weight属性使用初探

首先引用这篇文章:http://sinye.iteye.com/blog/1068204

文中对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不再贴了,效果依然很怪异:

实在不好意思,能力有限,贴到这里,我还没有总结出规律,先到此为止吧,您有什么好建议,可以发给我。

原文地址:https://www.cnblogs.com/tara/p/2094136.html