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>
效果图: