weightSum与weight

weight:

该控件能在屏幕中占据多大的空间(权重、比重)。

这个值越大,表明该控件可以在父控件中占据较多的“剩余”空间。默认的weight是0。

weightSum:

定义了weight 总和的最大值。
如果 android:weightSum 没有定义,那么默认值就是通过各个子类的 layout_weight 累加得到。

示例:

<?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="vertical">

    <!--
        weight:
        该控件能在屏幕中占据多大的空间。这个值越大,表明该控件可以在父控件中占据较多的“剩余”空间。默认的weight是0。
        weightSum:
        定义了weight 总和的最大值。
        如果 android:weightSum 没有定义,那么默认值就是通过各个子类的 layout_weight 累加得到
    -->

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:baselineAligned="false"
        android:orientation="horizontal">

        <!--
            假设屏幕的宽度为L
            实际宽度 = 控件原来的长度 + 剩余空间所占百分比的宽度
            LinearLayout1实际宽度 = 0 + (L-0-0) * 1/(1+2)= 1/3L
            LinearLayout2实际宽度 = 0 + (L-0-0) * 2/(1+2) = 2/3L
        -->

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@color/colorPrimary" />

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:background="@color/colorAccent" />
    </LinearLayout>


    <android.support.v4.widget.Space
        android:layout_width="match_parent"
        android:layout_height="5dp" />

    <!-- wrap_content 效果此处类似 0dp ,但不推荐此种写法-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:baselineAligned="false"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@color/colorPrimary" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:background="@color/colorAccent" />
    </LinearLayout>

    <android.support.v4.widget.Space
        android:layout_width="match_parent"
        android:layout_height="5dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:baselineAligned="false"
        android:orientation="horizontal">

        <!--
            假设屏幕的宽度为L
            实际宽度 = 控件原来的长度 + 剩余空间所占百分比的宽度
            LinearLayout1实际宽度 = L + [L-(L+L)] * 1/(1+2) = 2/3 L
            LinearLayout2实际宽度 = L + [L-(L+L)] * 2/(1+2) = 1/3 L
        -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@color/colorPrimary" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:background="@color/colorAccent" />
    </LinearLayout>


    <android.support.v4.widget.Space
        android:layout_width="match_parent"
        android:layout_height="5dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:baselineAligned="false"
        android:orientation="horizontal"
        android:weightSum="9">

        <!--
            假设屏幕的宽度为L
            实际宽度 = 控件原来的长度 + 剩余空间所占百分比的宽度
            LinearLayout1实际宽度 = L + [L-(L+L)] * 1/9 = 8/9 L
            LinearLayout2实际宽度 = L - 8/9L = 1/9L
        -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@color/colorPrimary" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:background="@color/colorAccent" />
    </LinearLayout>

    <android.support.v4.widget.Space
        android:layout_width="match_parent"
        android:layout_height="5dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:baselineAligned="false"
        android:orientation="horizontal"
        android:weightSum="9">

        <!--
            假设屏幕的宽度为L
            实际宽度 = 控件原来的长度 + 剩余空间所占百分比的宽度
            LinearLayout1实际宽度 = 0 + [L-(0+0)] * 1/9 = 1/9 L
            LinearLayout2实际宽度 = 0 + [L-(0+0)] * 2/9 = 2/9 L
        -->
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@color/colorPrimary" />

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:background="@color/colorAccent" />
    </LinearLayout>

</LinearLayout>

效果图:

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值