LinearLayout中的layout_weight屬性

LinearLayout中的layout_weight屬性

實驗結論:通過下面實驗可知:

(一) android:layout_height="fill_parent"

(二) android:layout_height="wrap_content"

在(一)、(二)2種情況在相同的weight屬性下呈現出正好相反的情況。


佈局LinearLayout代碼

Xml代碼  收藏代碼
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >    
  7. <Button  
  8.       android:layout_width="fill_parent"  
  9.       android:layout_height="fill_parent"  
  10.       android:text="Button1"  
  11.       android:background="#008000"  
  12.       android:layout_weight="0"  
  13. />  
  14. <Button  
  15.       android:layout_width="fill_parent"  
  16.       android:layout_height="fill_parent"  
  17.       android:text="Button2"  
  18.       android:background="#FFFF00"  
  19.       android:layout_weight="0"  
  20. />  
  21. </LinearLayout>  
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >    
  7. <Button  
  8.       android:layout_width="fill_parent"  
  9.       android:layout_height="fill_parent"  
  10.       android:text="Button1"  
  11.       android:background="#008000"  
  12.       android:layout_weight="0"  
  13. />  
  14. <Button  
  15.       android:layout_width="fill_parent"  
  16.       android:layout_height="fill_parent"  
  17.       android:text="Button2"  
  18.       android:background="#FFFF00"  
  19.       android:layout_weight="0"  
  20. />  
  21. </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屬性下呈現出正好相反的情況。

發佈了19 篇原創文章 · 獲贊 2 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章