当前位置: 移动技术网 > 移动技术>移动开发>Android > android:layout_weight属性详解

android:layout_weight属性详解

2019年04月03日  | 移动技术网移动技术  | 我要评论

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。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网