android中的weight

來源:互聯網
上載者:User

android:layout_weight詳細分析介紹:

布局檔案是:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns: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佔了2/3,button2佔了1/3。

但是如果將布局檔案中的button的屬性android:layout_width="fill_parent"改為android:layout_width="wrap_content"那麼出現的結果為:button1佔了1/3,button2佔了2/3。

 

出現這樣的結局是什麼意思呢?下面是詳細解釋:

linearLayout中包含有weight的child時,linearLayout會measure兩次:
設螢幕寬度為X
第一次:button1的measuredWidth為X,button2也為X (因為用了weight,所以linearLayout每次measure child時不考慮前一個已經佔用的大小),total_width為2X
第二次:計算delta=x-total_width=-x,然後會將button1的寬度設為
x+delta*1/3=0.66x, button2的寬度為 x+delta*2/3=0.33x


      那我現在對這句話重新概括一下:“因為設定了button1的權重最小,所以它佔用的布局優先順序就越高”,也就是說layout_weight越小,布局優先順序越高。也許在Android裡面布局並沒有優先順序之說,我這裡只是為了說明問題,自己定義的,所以朋友們不要拍磚。
      那首先分析一下當layout_width屬性設定為fill_parent的時候,即充滿父布局,當然意思是這個控制項要根據weight的設定儘可能的大,因此,依上例而論,button1的weight設為1,button2的weight設定為2.即button1的優先順序最高,因此,要填充父布局就要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做瞭解,要深深的理解下面兩句話:
在layout_width設定為fill_parent的時候,layout_weight所代表的是你的控制項要優先儘可能的大,但這個大是有限度的,即fill_parent.
在layout_width設定為wrap_content的時候,layout_weight所代表的是你的控制項要優先儘可能的小,但這個小是有限度的,即wrap_content.
layout_height 同 layout_width.

 

實際上上面做的解釋主要是對fill_parent和wrap_content做的解釋,我們可以參考下面的2篇博文知道,上面的解釋是存在一定的問題的。

 

一般來說,我們使用layout_weight,我們可能會使用在各個控制項或者布局(像LinearLayout),應用最廣泛的就是DV6300-T的主介面的布局,全部用的是layout_weight來控制的,這樣可以更好的適應不同的解析度。

 

所以我們經常用layout_weight來設定布局,一般是設定不同的LinearLayout為不同的layout_weight來完成基本布局,還可以利用LinearLayout來佔據空間都是沒有任何顯示,這樣的就是完全實現比重布局的LinearLayout,最後在各個LinearLayout裡面又實現多個控制項的layout_weight控制,同樣也可以使用只佔控制項無顯示的控制項來達到分配布局的效果,DV6300-T就是明顯例子。

還需要注意的就是LinearLayout的orientation如果沒有設定,那麼預設的就是horizontal水平方向。

 

按比例顯示LinearLayout內各個子控制項,需設定android:layout_width="0dp",

如果為豎直方向的設定android:layout_height="0dp"。

在這種情況下某子個控制項佔用LinearLayout的比例為:本控制項weight值 / LinearLayout內所有控制項的weight值的和。

 

一般我們控制比重都是設定對應方向的高或寬為0dp來實現我們想的比重控制,而很少用fill_parent或wrap_content,不過我們來理解這2種設定的區別。

 

還有一句話需要理解就是: 權重(layout_weight)最小,所以它佔用的布局優先順序就越高。

還需要注意的是儲存一致性控制:

如果我們要控制顯示比重,那麼我們一般要求各個等級的元素的控制方式必須一致,比如多個button控制項處於同一等級來控制比重布局,那麼如果設定的是android:layout_width="0dp"這樣的方式來控制,就必須把所有的等級控制項都用android:layout_width="0dp"來控制,而不能有的用android:layout_width="0dp",而還有的用android:layout_width=" fill_parent ",這樣就會出現混亂了。

 

下面還有一個例子:

<?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">

    <LinearLayout

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:layout_weight="1">

        <Button

           android:layout_width="fill_parent"

           android:layout_height="fill_parent"

           android:layout_weight="2"

           android:background="#00AAC0"

       />

       <Button

           android:layout_width="fill_parent"

           android:layout_height="fill_parent"

           android:layout_weight="2"

           android:background="#CCdd00"

           />

       <Button

           android:layout_width="fill_parent"

           android:layout_height="fill_parent"

           android:layout_weight="1"

           android:background="#0000FF"

       />    

    </LinearLayout>

    <LinearLayout

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:layout_weight="2">

       <Button

           android:layout_width="wrap_content"

           android:layout_height="fill_parent"

           android:layout_weight="2"

           android:background="#FFAA00"

       />

       <Button

           android:layout_width="wrap_content"

           android:layout_height="fill_parent"

           android:layout_weight="2"

           android:background="#CCdd00"

       />

       <Button

           android:layout_width="wrap_content"

           android:layout_height="fill_parent"

           android:layout_weight="1"

           android:background="#0000FF"

       />    

    </LinearLayout>

    <LinearLayout

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:layout_weight="2">

       <Button

           android:layout_width="0dp"

           android:layout_height="fill_parent"

           android:layout_weight="2"

           android:background="#00FF00"

       />

       <Button

           android:layout_width="0dp"

           android:layout_height="fill_parent"

           android:layout_weight="2"

           android:background="#CCdd00"

       />

       <Button

           android:layout_width="0dp"

           android:layout_height="fill_parent"

           android:layout_weight="1"

           android:background="#cc00FF"

       />    

    </LinearLayout>

</LinearLayout>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.