android 高度上分权重,Android LinearLayout weight权重使用

在日常的开发过程中,我们通常或多或少会使用到LinearLayout的weight属性来进行权重设置,进而达到按比例显示布局的意图

通常我们在使用时,会这样使用

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="horizontal">

android:layout_width="0dp"

android:layout_height="match_parent"

android:layout_weight="2"

android:background="@android:color/holo_blue_light" />

android:layout_width="0dp"

android:layout_height="match_parent"

android:layout_weight="1"

android:background="@android:color/holo_red_light" />

android:layout_width="0dp"

android:layout_height="match_parent"

android:layout_weight="1"

android:background="@android:color/holo_green_light" />

058b67b2d268?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

权重示例.png

我们一般使用weight属性时,会将width或者height设置为0dp,这时控件的宽或者高就会按照我们设置的权重,如上面的2:1:1来填充父控件

但是,如果子控件的宽度或高度不设置成0dp,那么他们的宽高是怎么分配的呢?

我们首先明确一点:weight权重是针对于LinearLayout的剩余空间,所以我们在设置该属性之后,LinearLayout会计算自己的剩余空间,然后将剩余空间按权重分配到子控件上。

以横向布局为例:

LinearLayout的剩余空间 = LinearLayout的宽度 - 各个子控件的宽度,可以有负值

以2个子控件为例,权重分别为2和1

那么

子控件1的宽度 = 子控件1的初始宽度 +(2/3)* LinearLayout的剩余空间

子控件2的宽度 = 子控件2的初始宽度 +(1/3)* LinearLayout的剩余空间

我们来验证一下:

为方便测量,使用px单位, 我们先设定父控件宽 = 800px, 子控件1 = 200px 子控件2 = 100px,weight都为1,我们自己先算一下,可以得出:

LinearLayout的剩余空间 = 800 - 200 - 100 = 500

子控件1宽度 = 200 + (1/2) * 500 = 450

子控件2宽度 = 100 + (1/2) * 500 = 350

看下效果是否和我们计算的一致:

android:layout_width="800px"

android:layout_height="match_parent"

android:orientation="horizontal">

android:layout_width="300px"

android:layout_height="match_parent"

android:layout_weight="1"

android:background="@android:color/holo_blue_light" />

android:layout_width="100px"

android:layout_height="match_parent"

android:layout_weight="1"

android:background="@android:color/holo_red_light"

android:singleLine="true" />

058b67b2d268?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

权重示例1.png

和我们预期的一致

我们再检测下"match_parent"的情况:

设定父控件宽 = 600px, 子控件1 ="match_parent" weight = "1" 子控件2 = "match_parent" weight = "2",我们自己先算一下,可以得出:

LinearLayout的剩余空间 = 600- 600- 600= -600

子控件1宽度 = 600+ (1 / 3) * (-600)= 400

子控件2宽度 = 600+ (2 / 3) * (-600)= 200

看下效果是否和我们计算的一致:

android:layout_width="600px"

android:layout_height="match_parent"

android:orientation="horizontal">

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_weight="1"

android:background="@android:color/holo_blue_light" />

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_weight="2"

android:background="@android:color/holo_red_light"

android:singleLine="true" />

058b67b2d268?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

权重示例2.png

和我们计算的完全一致

子控件"wrap_content"与确定值计算方式同上,有空的可以验证下

另外需要注意一点:weight是float类型

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值