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。

posted @ 2019-04-02 15:41  知亦行  阅读(4355)  评论(0编辑  收藏  举报