Android屬性系列之layout_weight

來源:互聯網
上載者:User

對自己現在所瞭解的layout_weight屬性進行記錄,不求全面,只求正確!

layout_weight意為"權重",我的理解就是給組件設定一個顯示大小的比例。
layout_weight設定一個值,會出現兩種情況。
第一種:當組件的“layout_width”屬性為“fill_parent”時,值越小,組件越大。
第二種:當組件的“layout_width”屬性為“wrap_content”時,值越大,組件越大。

第一種情況:
Xml代碼 
<LinearLayout  
    android:layout_weight="1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:layout_marginLeft="10dp" 
    android:layout_marginRight="10dp" 

    <Button  
        android:id="@+id/btn_save" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:text="儲存" 
    /> 
     
    <Button  
        android:id="@+id/btn_return" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:layout_weight="4" 
        android:text="返回" 
    /> 
</LinearLayout> 

在這裡"儲存"按鈕的Layout_weight=1,"返回"按鈕的Layout_weight=4,layout_width="fill_parent"時, 運行效果為:
 

第二種情況:
Xml代碼 
<LinearLayout  
    android:layout_weight="1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:layout_marginLeft="10dp" 
    android:layout_marginRight="10dp" 

    <Button  
        android:id="@+id/btn_save" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:text="儲存" 
    /> 
     
    <Button  
        android:id="@+id/btn_return" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="4" 
        android:text="返回" 
    /> 
</LinearLayout> 

在這裡"儲存"按鈕的Layout_weight=1,"返回"按鈕的Layout_weight=4,layout_width="wrap_content"時,運行效果為:
 

以上為本人對已知情況的總結,如有不對或總結不足,望指教!

作者“背著行囊獨自前行....”

相關文章

聯繫我們

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