LinearLayout中的layout_weight屬性
實驗結論:通過下面實驗可知:
(一) android:layout_height="fill_parent"
(二) android:layout_height="wrap_content"
在(一)、(二)2種情況在相同的weight屬性下呈現出正好相反的情況。
佈局LinearLayout代碼
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <Button
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:text="Button1"
- android:background="#008000"
- android:layout_weight="0"
- />
- <Button
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:text="Button2"
- android:background="#FFFF00"
- android:layout_weight="0"
- />
- </LinearLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <Button
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:text="Button1"
- android:background="#008000"
- android:layout_weight="0"
- />
- <Button
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:text="Button2"
- android:background="#FFFF00"
- android:layout_weight="0"
- />
- </LinearLayout>
layout_weight屬性如果沒有設置,則默認爲0.
按鈕1:button1,綠色
按鈕2:button2,黃色
下面以按鈕1和按鈕2的weight屬性(layout_weight)的變化,分別進行記錄。
這裏測試的是垂直佈局android:orientation="vertical",所以針對組件的android:layout_height屬性;如果測試的水平佈局,則需針對組件的android:layout_width屬性。
(一) android:layout_height="fill_parent"
button1(weight=0) button2(weight=0) | button1(weight=0) button2(weight=1) |
button1(weight=1) button2(weight=1) button1佔1/2,button2佔1/2 |
button1(weight=1) button2(weight=2) button1佔2/3,button2佔1/3 |
button1(weight=1) button2(weight=9) button1佔9/10,button2佔1/10 |
button1(weight=1) button2(weight=30) button1佔30/31,button2佔1/31 |
(二) android:layout_height="wrap_content"
button1(weight=0) button2(weight=0) |
button1(weight=0) button2(weight=1) |
button1(weight=1) button2(weight=1) button1佔1/2,button2佔1/2 |
button1(weight=1) button2(weight=2) button1佔1/3,button2佔2/3 |
button1(weight=1) button2(weight=9)
|
button1(weight=1) button2(weight=30)
|
通過以上實驗可知:
(一) android:layout_height="fill_parent"
(二) android:layout_height="wrap_content"
在(一)、(二)2種情況在相同的weight屬性下呈現出正好相反的情況。