分享

android:layout_weight属性详解

 小样样样样样样 2022-01-17

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。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约