android layout_weight 属性使用方法

layout_weight 是android 线性布局LinearLayout里 控件一个很好用的属性,

layout_weight : 即为当前线性布局 的属性指定方向(水平、竖直)上 剩余空间 的比重。

注意事项:
1. 布局为 线性布局LinearLayout时 控件的属性layout_weight才有效
2. 只有当前控件宽度属性为0 layout_weight 才有效 : 即android:layout_width=”0dp” layout_weight 才有效
3. 比重原则:线性布局中, 当前控件 占 所有控件(layout_weight有效)的比例

请看eg:

 <TextView
    android:id="@+id/textview"
    android:layout_width="200dp"
    android:layout_weight="2"
    android:layout_height="wrap_content"/>

<Button
    android:id="@+id/button1"
    android:layout_height="wrap_content"
    android:layout_width="0dp"
    android:layout_weight="2"
    android:text="button1"/>

<Button
    android:id="@+id/button2"
    android:layout_height="wrap_content"
    android:layout_width="0dp"
    android:layout_weight="5"
    android:text="button2"/> 

案例说明
textview
由于该控件的 layout_width 指定为 200dp 所以 layout_weight属性是无效的 该控件仍然按照layout_width布局

button1 和button2
由于这两个按钮的属性layout_width 都指定为0 所以layout_weight有效
button1 和button2 占据了线性布局 除去textview 剩余的部分
button1 所占宽度比例 2/(2+5)
button2 所占宽度比例 5/(2+5)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值