LinearLayout的layout_weight属性的计算

layout_weight表示view分配到的额外的布局空间,可正可负。

布局的时候,系统先按照view的layout_width和layout_height来布局,然后再根据layout_weight对view的位置进行调整。

举例来说

<?xml version="1.0" encoding="utf-8"?>
<layout
    xmlns:android="http://schemas.android.com/apk/res/android">
    <data></data>
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#00f"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#f00"
            android:layout_weight="1"/>

    </LinearLayout>
</layout>

这样一个布局,由于LinearLayout是vertical的,layout_weight作用在vertical上,系统在布局的时候:

1. 根据layout_height,三个view都是match_parent,所以三个view的高度都是 parent-height,总高度为parent-height * 3,viewGroup原有的空间为parent-height,因此余下的空间为-parent-height * 2。

2. 根据layout_weight分配余下的空间。layout_weight的和为3,单个view的layout_weight为1,所以分到的layout_height为-parent-height * 2 * 1/3 = -parent-height * 2/3。

3. 两次分配的layout_height相加,得到最终的layout_height = parent-height - parent-height * 2/3 = parent-height / 3;


如果viewGroup设置了weightSum,则优先以weightSum作为view分配额外空间时的分母。如果weightSum=3,则与上述情况一致。考虑一下冲突的情况。

首先,令weightSum = 2,preview变成这样


还是按照上面的分析,粗体部分是区别,系统布局的时候是这样:

1. 根据layout_height,三个view都是match_parent,所以三个view的高度都是 parent-height,总高度为parent-height * 3,viewGroup原有的空间为parent-height,因此余下的空间为-parent-height * 2。

2. 根据layout_weight分配余下的空间。layout_weight的和被设置为layoutSum = 2,单个view的layout_weight为1,所以按照LinearLayout的线性顺序,前两个View就占据了所有的weightSum,只有它们能分配额外的布局空间,第三个View不分配额外的布局空间,前两个View分到的layout_height为-parent-height * 2 * 1/2 = -parent-height。

3. 两次分配的layout_height相加,得到最终的layout_height,前两个View的layout_height = parent-height - parent-height = 0,所以不显示,第三个View的layout_height = parent-height + 0 = parent-height,所以占满ViewGroup。


然后,令weightSum = 4,preview变成这样


还是按照上面的分析,粗体部分是区别,系统布局的时候是这样:

1. 根据layout_height,三个view都是match_parent,所以三个view的高度都是 parent-height,总高度为parent-height * 3,viewGroup原有的空间为parent-height,因此余下的空间为-parent-height * 2。

2. 根据layout_weight分配余下的空间。layout_weight的和被设置为layoutSum = 4,单个view的layout_weight为1,所以按照LinearLayout的线性顺序,三个View都能分配额外的布局空间,每个View分到的layout_height为-parent-height * 2 * 1/4 = -parent-height / 2。

3. 两次分配的layout_height相加,得到最终的layout_height,每个View的layout_height = parent-height - parent-height / 2  = parent-height / 2,按照LinearLayout的线性顺序,前两个View就占满了viewGroup,所以第三个View就看不到了。


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值