LinearLayout的(de)權(quan)重屬性
時間:2018-09-25 來源:未知
LinearLayout子控(kong)件重要的屬性 android:layout_weight, 值為整數, 默認為0
1 當父控件LinearLayout中android:orientation="vertical"時
子(zi)控(kong)(kong)件(jian)(jian)高(gao)度(du)(du) = 子(zi)控(kong)(kong)件(jian)(jian)原本高(gao)度(du)(du) + (父控(kong)(kong)件(jian)(jian)LinearLayout高(gao)度(du)(du) - 所有子(zi)控(kong)(kong)件(jian)(jian)高(gao)度(du)(du)之和) * 子(zi)控(kong)(kong)件(jian)(jian)高(gao)度(du)(du)權重比
2 當父控件LinearLayout中android:orientation="horizontal",
子控件(jian)(jian)寬度(du)(du) = 子控件(jian)(jian)原(yuan)本寬度(du)(du) + (父控件(jian)(jian)LinearLayout寬度(du)(du) - 所(suo)有子控件(jian)(jian)寬度(du)(du)之和) * 子控件(jian)(jian)寬度(du)(du)權(quan)重比(bi)
(match_parent或fill_parent都指父(fu)控(kong)件高度(du)或寬度(du))
例1:
<LinearLayout xmlns:android="//schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="#f00"
android:background="#0f0"
android:text="上面"
android:textSize="40sp"
android:layout_weight="1"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="#0f0"
android:background="#00f"
android:text="中(zhong)間"
android:textSize="40sp"
android:layout_weight="1"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="#0f0"
android:background="#f00"
android:text="下面"
android:textSize="40sp"
android:layout_weight="1"
/>
</LinearLayout>
---->子控件高(gao)度(du) = 子控件原本高(gao)度(du) + (父(fu)控件LinearLayout高(gao)度(du) - 所有子控件高(gao)度(du)之和) * 子控件高(gao)度(du)權重比
上面第個(ge)子(zi)控件TextView的高度(du)
= 1屏高(gao) + [ (1屏高(gao) - 3屏高(gao)) * 1/(1+1+1) ]
= 1屏(ping)高(gao) + [-2屏(ping)高(gao)*(1/3) ]
= 1屏高(gao) - 2屏高(gao)*(1/3)
= 1屏(ping)高(gao) - 2/3屏(ping)高(gao)
= 1/3 屏高

例2:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="//schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:text="one"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1.0"
android:text="two"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:text="three"/>
</LinearLayout>
分析
**使用(yong)公式(shi)---->子(zi)控(kong)件高度 = 子(zi)控(kong)件原本高度 + (父控(kong)件LinearLayout高度 - 所有子(zi)控(kong)件高度之(zhi)和) * 子(zi)控(kong)件高度權重比
**(1) one的高度 = one高度 + [1屏高 - (one高度+two高度+three高度) ] * 0/(1+0+0)
= one高度(du) + 0
= one高度
(2)two的高(gao)(gao)(gao)(gao)度(du) = two高(gao)(gao)(gao)(gao)度(du) +[1屏高(gao)(gao)(gao)(gao) - (one高(gao)(gao)(gao)(gao)度(du)+two高(gao)(gao)(gao)(gao)度(du)+three高(gao)(gao)(gao)(gao)度(du)) ] * 1/(1+0+0)
= one高(gao)(gao)度(du) +[1屏高(gao)(gao) - (one高(gao)(gao)度(du)+one高(gao)(gao)度(du)+one高(gao)(gao)度(du)) ]
= one高度 +[1屏高 - 3one高度]
= 1屏(ping)高(gao) - 2one高(gao)度
(3)three的高度(du)(du) = one高度(du)(du) + [1屏(ping)高 - (one高度(du)(du)+two高度(du)(du)+three高度(du)(du)) ] * 0/(1+0+0)
= one高度 + 0
' = one高度(du)(du) = three高度(du)(du)

例3:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="//schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:background="#ff0000"
android:layout_width="**"
android:layout_height="wrap_content"
android:text="1"
android:textColor="@android:color/white"
android:layout_weight="1"/>
<TextView
android:background="#cccccc"
android:layout_width="**"
android:layout_height="wrap_content"
android:text="2"
android:textColor="@android:color/black"
android:layout_weight="2" />
<TextView
android:background="#ddaacc"
android:layout_width="**"
android:layout_height="wrap_content"
android:text="3"
android:textColor="@android:color/black"
android:layout_weight="3" />
</LinearLayout>
分析:
**使(shi)用公式子(zi)控(kong)(kong)件寬(kuan)(kuan)度 = 子(zi)控(kong)(kong)件原本寬(kuan)(kuan)度 + (父控(kong)(kong)件LinearLayout寬(kuan)(kuan)度 - 所有子(zi)控(kong)(kong)件寬(kuan)(kuan)度之和(he)) * 子(zi)控(kong)(kong)件寬(kuan)(kuan)度權重比
** 當(dang)三(san)個android:layout_width="**"都改為(wei)android:layout_width="wrap_content"時, 三(san)個TextView的原本寬度一樣寬,該寬度下面使用X表示

第1個TextView寬度 = X + (1屏寬 - 3X) * 1/(1+2+3)
= X + (1屏寬 - 3X) * 1/6
=X- (1/2)X + (1/6)屏寬(kuan)
= (1/2)X + (1/6)屏寬(kuan)
第2個TextView寬度(du) = X+ (1屏寬 - 3X) * 2/(1+2+3)
= X+ (1屏寬 - 3X) * 1/3
= X- X+ (1/3)屏寬
= (1/3)屏寬
第3個TextView寬度 = X+ (1屏(ping)寬 - 3X) * 3/(1+2+3)
= X + (1屏寬 - 3X) * 1/2
= X- (3/2)X + (1/2)屏寬
= (1/2)屏(ping)寬 - (1/2)X
當三個android:layout_width="**"都改為(wei)android:layout_width="fill_parent"時, 并把(ba)原android:layout_weight改為(wei) 1, 2, 2
三個TextView的(de)原本(ben)寬度一(yi)樣寬,該寬度都是一(yi)個屏寬, 使用W表示(shi)

第1個TextView寬度 = W + (W - 3W) * 1/(1+2+2)
= W + (W - 3W) * 1/5
= W - 2W * (1/5)
= W-(2/5)W
= (3/5)W
第2個TextView寬(kuan)度(du) = W + (W - 3W) * 2/(1+2+2)
= W + (W - 3W) * 2/5
= W - 2W * (2/5)
= W-(4/5)W
= (1/5)W
第3個TextView寬度 = W + (W - 3W) * 2/(1+2+2)
= W + (W - 3W) * 2/5
= W - 2W * (2/5)
= W-(4/5)W
= (1/5)W
當(dang)三個android:layout_width="**"都改為(wei)(wei)android:layout_width="fill_parent"時, 并把原android:layout_weight改為(wei)(wei) 1, 2, 3
三個TextView的原本寬(kuan)度(du)一樣寬(kuan),該寬(kuan)度(du)都是一個屏寬(kuan), 使用1W表示

第1個TextView寬度 = W + (W - 3W) * 1/(1+2+3)
=W + (W - 3W) * 1/6
=W - 2W * (1/6)
=W-(1/3)W
= (2/3)W
第2個TextView寬(kuan)度 = W + (W - 3W) * 2/(1+2+3)
= W + (W - 3W) * 2/6
= W - 2W * (1/3)
= W-(2/3)W
= (1/3)W
第3個TextView寬(kuan)度 = W + (W - 3W) * 3/(1+2+3)
= W + (W - 3W) * 1/2
= W - 2W * (1/2)
= W- W
=0

