例如:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#00ff00"
android:weightSum="0"
android:orientation="horizontal" >
<Button
android:id="@+id/imageViewLoginState"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="1" >
</Button>
<Button
android:id="@+id/imageViewLoginState1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="2" >
</Button>
<Button
android:id="@+id/imageViewLoginState2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="2"
android:text="3" >
</Button>
</LinearLayout>
假设Container即LinearLayout的宽度为PARENT_WIDTH
三个按钮的宽度都是FILL_PARENT,所以在应用layout_weight之前,三个按钮的宽度都为PARENT_WIDTH
所以额外空间: delta = PARENT_WIDTH - 3 * PARENT_WIDTH = -2 * PARENT
因为LinearLayout没有设置android:weightSum(默认为0,设置为0就当没设置吧),所以 mWeightSum = 1 + 1 +2 =4
所以:
第一个按钮的宽度为:
PARENT_WIDTH + share = PARENT_WIDTH + (layout_weight * delta / mWeightSum)
= PARENT_WIDTH + (1 * (-2 * PARENT_WIDTH) /4) = 1 /2 *PARENT_WIDTH
然后更新weightSum与delta:
weightSum -= childExtra;(=3)
delta -= share;(=-3/2 * PARENT_WIDTH)
第二个按钮的宽度为:ARENT_WIDTH + share = PARENT_WIDTH + (layout_weight * delta / mWeightSum)
= PARENT_WIDTH + (1 * (-3 / 2 * PARENT_WIDTH) /3) = 1 /2 *PARENT_WIDTH
更新weightSum与delta:
weightSum -= childExtra;(=2)
delta -= share;(=-PARENT_WIDTH)
第三个按钮的宽度为:
PARENT_WIDTH + share = PARENT_WIDTH + (layout_weight * delta / mWeightSum) = PARENT_WIDTH + (2 * (- PARENT_WIDTH) /2) = 0
所以最终的而已就是前两个按钮平分LinearLayout,第三个按钮消失了.
举例:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:background="#00ff00"
android:orientation="horizontal" >
<Button
android:id="@+id/button1"
android:layout_width="60dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="1" >
</Button>
<Button
android:id="@+id/button2"
android:layout_width="60dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="2" >
</Button>
<Button
android:id="@+id/button3"
android:layout_width="60dp"
android:layout_height="fill_parent"
android:layout_weight="2"
android:text="3" >
</Button>
</LinearLayout>
额外空间:delta = 100- 3 * 60 = -80
mWeightSum = 1 + 1 +2 =4
所以:
第一个按钮的宽度为:60+ share = 60 + (layout_weight * delta / mWeightSum) = 60 + (1 * (-80) /4) = 40
然后:weightSum -= childExtra;(=3)
delta -= share;(=-60)
第二个按钮的宽度为:0 + share = 60 + (layout_weight * delta / mWeightSum) = 60 + (2 * (-60) /3)= 40
然后:weightSum -= childExtra;(=2)
delta -= share;(=-40)
第三个按钮的宽度为:
60 + share = 60 + (layout_weight * delta / mWeightSum)
= 60 + (2 * (-40) /2)
= 20
例外:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:background="#00ff00"
android:orientation="horizontal" >
<Button
android:id="@+id/button1"
android:layout_width="60dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="1" >
</Button>
<Button
android:id="@+id/button2"
android:layout_width="40dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="2" >
</Button>
</LinearLayout>
额外空间为100,所以Button1的宽度为60+100/2=110,Button2的宽度为40+100/2=90。
参考:http://www.cnblogs.com/angeldevil/archive/2012/04/08/2437747.html