Android中layout_weight和weightSum

weightSum是LinearLayout的属性,定义weight的总和。如果未指定该值,以LinearLayout中所有子视图的layout_weight属性的累加值作为总和。

layout_weight是LinearLayout中的子视图使用有效的属性,用于将剩余空间按照比例分配给子视图。

LinearLayout空间分配规则如下(分为两步):
第一步:
先按照LinearLayout中各个子视图的layout_width(layout_height)的设置的值来给子视图分配空间。

第二步:
然后剩余的空间按照layout_weight的比例再分配给各个子视图。

注意:
这里的比例就是是 layout_weight/weightSum
(如果LinearLayout没有设置weightSum的值,那么weightSum默认就是LinearLayout中所有子视图的layout_weight的和)

实例1:

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="1"
        android:background="#FF0000"
        ></TextView>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="2"
        android:background="#339966"
        ></TextView>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="3"
        android:background="#0033FF"
        ></TextView>
</LinearLayout>

效果图如下:

分析一下空间分配过程:

第一步:按照三个TextView的设置的layout_width分别给这三个TextView给分配空间:
TextView1: match_parent
TextView2: match_parent
TextView3:match_parent

第二步:将剩余空间按照三个TextView的比例再分配给三个TextView
三个TextView分别都已经分配了match_parent
所以剩余空间是:match_parent-3*(match_parent)=-2*match_parent

三个TextView的layout_weight分别为:1,1,1
所以三个TextView剩余空间所占的比例都是1/(1+1+1)=1/3
所以剩余空间三个TextView分别分配到 -2/3*(match_parent)

所以最终三个TextView分配到的空间是:
TextView1: match_parent-2/3*(match_parent)=1/3*(match_parent)
TextView2: match_parent-2/3*(match_parent)=1/3*(match_parent)
TextView3:match_parent-2/3*(match_parent)=1/3*(match_parent)

三个TextView都占屏幕的1/3;

实例2:

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#FF0000"
        android:gravity="center"
        android:text="1"></TextView>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:background="#339966"
        android:gravity="center"
        android:text="2"></TextView>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:background="#0033FF"
        android:gravity="center"
        android:text="3"></TextView>
</LinearLayout>

将上面的实例改了一下,三个TextView的layout_weight分别改成1,2,3
显示效果如下:

分析一下空间分配过程:

第一步:

三个TextView按照layout_width的设置,分别分配到match_parent

第二步:

剩余空间是: -2*match_parent

三个TextView剩余空间所占的比例分别为:1/6,2/6,3/6
所以剩余空间分别分配到-1/3*(match_parent),-2/3*(match_parent),
-match_parent

所以最终三个TextView分配到的空间分别是:
TextView1:match_parent-1/3*(match_parent)=2/3*(match_parent)
TextView2:match_parent-2/3*(match_parent)=1/3*(match_parent)
TextView3:match_parent-match_parent=0

TextView3分配到的空间为0,所以TextView3没有显示

一般我们使用的时候,如果是横向排列的话,如果子视图设置了
layout_weight,那么子视图的layout_width设为0。
如果是纵向排列的话,layout_height设为0,即完全交由比例分配。
这是Google推荐的用法。
如下实例3所示:

实例3:

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

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#FF0000"
        android:gravity="center"
        android:text="1"></TextView>

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:background="#339966"
        android:gravity="center"
        android:text="2"></TextView>

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:background="#0033FF"
        android:gravity="center"
        android:text="3"></TextView>
</LinearLayout>

显示效果如下:

三个TextView的layout_width均设为0,这就很简单的把三个TextView按照1:2:3的比例分配了空间。

分析一下空间分配过程:
第一步:

三个TextView的layout_width均为0,所以分配到的空间均为0

第二步:

剩余空间是:match_parent

三个TextView的layout_weight分别为:1,2,3
所以分别分配到剩余空间的1/6,2/6,3/6

所以最终三个TextView分配到的空间是:
TextView1: 1/6*(match_parent)
TextView2: 2/6*(match_parent)
TextView3: 3/6*(match_parent)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值