Android佈局之weight屬性解析

在網上看了一些對Layout_weight的講解,有些說的比較片面,只列舉了一種情況,然後自己通過實驗和一些比較好的文章總結了一下,特此記錄下來,以備以後所用。Layout_weight是線性佈局,也就是LinearLayout裏面用到的,下面通過實驗來看這個Layout_weight的特性。

1.當控件的屬性android:layout_width="fill_parent"時,佈局文件如下:

Xml代碼  

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="horizontal" android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent">  
  5.     <Button android:layout_width="fill_parent"  
  6.         android:layout_height="wrap_content" android:layout_weight="1"  
  7.         android:text="Button1" />  
  8.     <Button android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content" android:layout_weight="2"  
  10.         android:text="Button2" />  
  11. </LinearLayout>  

<?xmlversion="1.0" encoding="utf-8"?>

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

  android:orientation="horizontal"android:layout_width="fill_parent"

  android:layout_height="fill_parent">

  <Button android:layout_width="fill_parent"

    android:layout_height="wrap_content"android:layout_weight="1"

    android:text="Button1" />

  <Button android:layout_width="fill_parent"

    android:layout_height="wrap_content"android:layout_weight="2"

    android:text="Button2" />

</LinearLayout>

 在這裏Button1的Layout_weight=1,Buttong2的Layout_weight=2,運行效果爲:


我們看到,Button1佔了2/3,Button2佔了1/3。如果此時把button2的weight設置成2000,不是說Button2就消失了,而是Button1的寬度幾乎佔滿了屏幕寬度,而屏幕最後一絲細條則是留給Button2的,已近非常小了,沒有顯示出來。截圖如下:



 

 得出結論在layout_width設置爲fill_parent的時候,layout_weight代表的是你的控件要優先儘可能的大,但儘可能大是有限度的,即fill_parent.

 

2.當控件的屬性android:layout_width="wrap_content"時,佈局文件如下:

Xml代碼  

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="horizontal" android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent">  
  5.     <Button android:layout_width="wrap_content"  
  6.         android:layout_height="wrap_content" android:layout_weight="1"  
  7.         android:text="Button1" />  
  8.     <Button android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content" android:layout_weight="2"  
  10.         android:text="Button2" />  
  11. </LinearLayout>  

<?xmlversion="1.0" encoding="utf-8"?>

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

  android:orientation="horizontal"android:layout_width="fill_parent"

  android:layout_height="fill_parent">

  <Button android:layout_width="wrap_content"

    android:layout_height="wrap_content"android:layout_weight="1"

    android:text="Button1" />

  <Button android:layout_width="wrap_content"

    android:layout_height="wrap_content"android:layout_weight="2"

    android:text="Button2" />

</LinearLayout>

 同樣,Button1的weight設置爲1,Button2的weight設置爲2,但是效果與fill_parent的效果截然相反。運行效果如下:


這時,和fill_parent正好相反,Button1的寬度佔據了屏幕寬度的1/3,而Button2的寬度佔據了屏幕的2/3,如果此時把Button1的weight設置爲2000,按照之前理解,Button1應該小的幾乎在屏幕上看不到,但是錯了,實驗告訴我們,當Button1的weight非常小時,也要"wrap_content",也就是要保證Button1至少能夠顯示。以下是Button1設置weight爲2000時的運行截圖:


我們看到,Button1已經足夠小,但是要保證他能顯示出來,因此得出結論:

linearLayout中包含有weightchild時,linearLayoutmeasure兩次:
設屏幕寬度爲X
第一次:button1measuredWidthX,button2也爲X (因爲用了weight,所以linearLayout每次measure child時不考慮前一個已經佔用的大小)total_width2X
第二次:計算delta=x-total_width=-x,然後會將button1的寬度設爲
x+delta*1/3=0.66x, button2的寬度爲 x+delta*2/3=0.33x
 那我現在對這句話重新概括一下:“因爲設置了button1的權重最小,所以它佔用的佈局優先級就越高”,也許在Android裏面佈局並沒有優先級之說,我這裏只是爲了說明問題,自己定義的,所以朋友們不要拍磚。     

 那首先分析一下當layout_width屬性設置爲fill_parent的時候,即充滿父佈局,當然意思是這個控件要根據weight的設置儘可能的大,因此,依上例而論,button1的weight設爲1,button2的weight設置爲2.即button的優先級最高,因此,要填充父佈局就要button1先來填充,儘可能的大,那這個儘可能又是多少呢,這就要綜合layout裏其他控件的weight值了,然後做一下運算,button1佔據2/3,button2佔據1/3.你也可以把button2設置爲一個非常大的數,比如2000,此時在Graphical Layout模式下可以看到button1填充滿了整個寬度,而看不到button2的影子,事實上button2還是存在的,你把鼠標指向button1的後面就可以看到一個長長的豎條,那個就是button2,已經非常非常小了。因此,layout_width設置爲fill_parent的時候,weight所代表的是你的控件要優先儘可能的大。      

接着是當layout_weight設置爲wrap_content的時候,即適應內容的寬度,意思是這個控件要儘可能的小,只要能把內容顯示出來就可以了,同樣的,如果把button1和button2的layout_weight設置爲wrap_content後,button1的weight爲1,button2的weight爲2.那麼button1要優先儘可能的小,而button2也要儘可能的小,只是優先級不一樣,因爲設置了weight,所以這兩個控件總的寬度要填滿父佈局的寬度,所以就又要計算每個控件所佔據的大小,此時,button1的優先級較高,共有兩份,一份1/3,一份2/3,button1要儘可能的小,那button1當然要選1/3,因此,我們看到的效果反而是button2佔據的較大。這裏要說的是如果把權值同樣做如下設置:button1爲1,button2爲2000,那button1是不是就要佔據1/2000的空間呢?這麼理解就錯了,剛纔說了,要儘可能的小,但這個小是有一個限度的,那就是wrap_content,就是還要是內容完完整整的顯示出來,同樣的,儘可能的大也是有一個限度的,那就是父佈局的寬度。因此,layout_width設置爲wrap_content的時候,weight所代表的是你的控件要優先儘可能的大。   

所以,要對weight做了解,要深深的理解下面兩句話:

所以,要對weight做了解在layout_width設置爲fill_parent的時候,layout_weight所代表的是你的控件要優先儘可能的大,但這個大是有限度的,fill_parent.
layout_width設置爲wrap_content的時候,layout_weight所代表的是你的控件要優先儘可能的小,但這個小是有限度的,wrap_content.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章