layout_weight使用

layout_weight的真正含义是等比例分割剩余空间,

在线性布局(linearlayout)中对宽度和高度进行比例分割,

三个子linearlayout分割父linearlayout高度,代码如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.sunjianfei.linearlayout_test.MainActivity"
    android:orientation="vertical"
    android:id="@+id/linearall">


    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:id="@+id/linear1"
        android:background="#ff0000"
        android:gravity="center_horizontal">
    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:id="@+id/linear2"
        android:background="#aa10ff"
        >

    </LinearLayout>
   
   <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="3"
        android:id="@+id/linear3"
        android:background="#000000"

        >
    </LinearLayout>
</LinearLayout>

三个linearlayout的比为1:2:3;

三个linearlayout的layout_height为"match_parent",那么剩余空间为1P-3P=-2P;

第一个linearlayout的高度:P+1/6*(-2P)=2/3P;

第二个linearlayout的高度:P+2/6*(-2P)=1/3P;

第三个linearlayout的高度:P+3/6*(-2P)=0;

所以三个linearlayout的实际高度比为2:1:0;

如果是两个linearlayout的比为1:2;

也是同样的计算,

剩余空间:P-2P=-P;

第一个linearlayout的高度:P+1/3*(-P)= 2/3P;

第二个linearlayout的高度:P+2/3*(-P)= 1/3P;

如果是四个五个平分也是同样的计算方法:

高度/宽度 = P+权*剩余空间

 

posted @ 2016-09-06 10:48  有棱角的圆  阅读(165)  评论(0编辑  收藏  举报