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