android:layout_weight屬性詳解


weight:重量、權重。

  當我們給一個view設置了android:layout_weight屬性,意味着賦予它話語權,常規思維就是誰的weight大,誰說了算(空間占比大)。

 

  下面我們來看下具體的代碼:

  

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <Button
        android:text="我的weight是2"
        android:layout_weight="2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我的weight是1"
        android:layout_weight="1"/>

</LinearLayout>

 

 

 

兩個Button的寬度設置的是wrap_content,此時weight屬性使LinearLayout給內部的兩個Button按2:1的權重分配了所有寬度空間。

官方推薦使用weight按比例分配寬度空間時,將LinearLayout內View的寬度設置為0。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM