Android——android weight 屬性(百度)

來源:互聯網
上載者:User

標籤:

LinearLayout 在androidUI布局中使用非常多,它其中有個很方便又很有意思的屬性 weight 

,這個屬性理解起來不是那麼簡單的,而真正理解了又覺得非常簡單!

下面就通過一個例子來說明.

 

weight代表的含義--- android:layout_width

布局代碼是:

 

<?xml version="1.0" encoding="utf-8"?>

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

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="horizontal" >

 

    <Button

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_weight="1"

        android:text="button1" />

    <Button

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_weight="5"

        android:text="button2" />

</LinearLayout>

分析:

  當android:layout_width="match_parent"的時候,如果設定了weight屬性,那麼根據它的weight值(可以理解為優先順序)來佔據空間,而且這個值是越小,占的空間越大,因此此時可以理解為優先順序.

比如:按鈕1和按鈕2的width屬性都是match_parent,如果按鈕1的weight= 1 按鈕2的為weight = 2  那麼按照優先順序 按鈕1先佔據,按鈕2後佔據. 大小比例為 

按鈕1 = 2/(1+2) ,按鈕2 = 1/(1+2) 如下第一幅圖

如果按鈕1的weight我們設定為1000,按鈕2的weight設定為1 那麼 按鈕2 幾乎全部佔據了所有空間!如第二幅

weight代表的含義--- android:layout_width
  1.  

    注意!weight的含義將發生根本行的變化!

    先看代碼

     

    <?xml version="1.0" encoding="utf-8"?>

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

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:orientation="horizontal" >

        <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="1000"

            android:text="button2" />

    </LinearLayout>

 

當 android:layout_width="wrap_content"的時候,我們發現即使我們將按鈕2設定成: android:layout_weight="1000"  按鈕2不但沒有像第一種情況下那樣消失,反而佔據了更多的空間,這是怎麼回事兒呢?

分析: 主要的變化來自於我們設定了android:layout_width="wrap_content",也就是說一個控制項在寬度上只會包裹它的內容. 如果設定上了權重,意思告訴該控制項,要根據weight來儘可能的包裹內容,weight值越小,包裹越小.值越大,包裹越大. 但是再小,控制項都要能包裹內容. 因此,不會像第一種情況那樣消失!

正確使用weight 屬性!
  1.  

    下面是來自SDK的一句話:

    In order to improve the layout efficiency when you specify the weight, you should change the width of theEditText to be zero (0dp). Setting the width to zero improves layout performance because using "wrap_content"as the width requires the system to calculate a width that is ultimately irrelevant because the weight value requires another width calculation to fill the remaining space.

    它的大致意思就是說: 我們如果在某個方向上使用了weight ,那麼我們必須在對應的方向上將width設定為0dp. 它告訴了我們設定為0dp是因為使用weight,系統是採用了另外一套計算佔用空間大小的演算法的.(the weight value requires another width calculation to fill the remaining space.)

     

  2. 2

    總結: 要正確使用weight,不要再去糾結 

    android:layout_width="match_parent"

     android:layout_width="wrap_content" 兩種情況下該如何設定weight. 因為這樣設定根本就是錯誤的用法.

    正確的用法是:

    先設定 android:layout_width="0dp" 或者 android:layout_height="0dp"

    然後再去調配權重

    而此時的weight也非常好理解: weight就是比重!比例!請看

Android——android weight 屬性(百度)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.