android:layout_weight 属性怎么用,Android layout_weight 属性使用介绍

解释:layout_weight  参数为整型值 ,它的值用于指定父控件空闲空间的分配比例。

下面举例说明

下图中红色部分即为空闲空间

0818b9ca8b590ca3270a3433284dd417.png

相关代码:

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="vertical"

android:background="#ff0000" >

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal" >

android:id="@+id/textView1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="#00ff00"

android:textSize="30sp"

android:text="first" />

android:id="@+id/textView2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="#ffff00"

android:textSize="30sp"

android:text="second" />

设置两个TextView的layout_weight属性为1

0818b9ca8b590ca3270a3433284dd417.png

如图,first和second占满了父控件控件,layout_weight都设置为1的意义为:

将空闲空间平均分为2份,first和second各占一份,并不是将整个父控件分为2份

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="#ff0000"

android:orientation="vertical" >

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal" >

android:id="@+id/textView1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="#00ff00"

android:text="first"

android:textSize="30sp" />

android:id="@+id/textView2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="#ffff00"

android:text="second"

android:textSize="30sp" />

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_marginTop="3dp"

android:orientation="horizontal" >

android:id="@+id/textView3"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="1"

android:background="#00ff00"

android:text="first"

android:textSize="30sp" />

android:id="@+id/textView4"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="1"

android:background="#ffff00"

android:text="second"

android:textSize="30sp" />

最后分享一个小技巧,如果想first占父控件的三分之一,second占父控件的三分之二

将控件宽度设置为0,layout_weight设置为相应的比例即可

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_marginTop="3dp"

android:orientation="horizontal" >

android:id="@+id/textView3"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

android:background="#00ff00"

android:text="first"

android:textSize="30sp" />

android:id="@+id/textView4"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="2"

android:background="#ffff00"

android:text="second"

android:textSize="30sp" />

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值