现在的位置: 首页 > 综合 > 正文

Android layout_weight理解

2018年02月04日 ⁄ 综合 ⁄ 共 5366字 ⁄ 字号 评论关闭

public float weight

Added in API level 1

Indicates how much of the extra space in the LinearLayout will be allocated to the view associated with these LayoutParams. Specify 0 if the view should not be stretched. Otherwise the extra pixels
will be pro-rated among all views whose weight is greater than 0.

表明了在LinearLayout中,Weight决定了可以分配给View的空间。如果为0(默认)View不拉伸,其他情况下按照Weight去分配View的空间。

既然表明与LinearLayout.LayoutParams有关

public LinearLayout.LayoutParams (int width, int height, float weight)

结合Width,Height,Weight做了如下图的简单demo,表明并不是weight小的,占用的空间一定大。

六个Demo依次的顺序为:

1.

android:orientation="horizontal"(父线性容器)
android:layout_width="match_parent"
android:layout_height="match_parent"

2.

android:orientation="horizontal"(父线性容器)
android:layout_width="0dp"

android:layout_height="match_parent"


3.

android:orientation="vertical"(父线性容器)
android:layout_width="match_parent"
android:layout_height="match_parent"


4.

android:orientation="vertical"(父线性容器)
android:layout_width="match_parent"
android:layout_height="0dp"

5.

android:orientation="horizontal"(父线性容器)
android:layout_width="match_parent"
android:layout_height="wrap_content"


6.

android:orientation="vertical"(父线性容器)
android:layout_width="wrap_content"

android:layout_height="match_parent"

<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"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="3dp"
        android:layout_weight="1"
        android:background="#999555"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#123456"
            android:gravity="center"
            android:text="weight = 1"
            android:textColor="#FFFFFF" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:background="#654321"
            android:gravity="center"
            android:text="weight = 2"
            android:textColor="#FFFFFF" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="3dp"
        android:layout_weight="1"
        android:background="#999555"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#123456"
            android:gravity="center"
            android:text="weight = 1"
            android:textColor="#FFFFFF" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:background="#654321"
            android:gravity="center"
            android:text="weight = 2"
            android:textColor="#FFFFFF" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="3dp"
        android:layout_weight="1"
        android:background="#999555"
        android:orientation="vertical" >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#123456"
            android:gravity="center"
            android:text="weight = 1"
            android:textColor="#FFFFFF" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:background="#654321"
            android:gravity="center"
            android:text="weight = 2"
            android:textColor="#FFFFFF" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="3dp"
        android:layout_weight="1"
        android:background="#999555"
        android:orientation="vertical" >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="#123456"
            android:gravity="center"
            android:text="weight = 1"
            android:textColor="#FFFFFF" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="2"
            android:background="#654321"
            android:gravity="center"
            android:text="weight = 2"
            android:textColor="#FFFFFF" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="3dp"
        android:layout_weight="1"
        android:background="#999555"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#123456"
            android:gravity="center"
            android:text="weight = 1"
            android:textColor="#FFFFFF" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:background="#654321"
            android:gravity="center"
            android:text="weight = 2"
            android:textColor="#FFFFFF" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="3dp"
        android:layout_weight="1"
        android:background="#999555"
        android:orientation="vertical" >


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#123456"
            android:gravity="center"
            android:text="weight = 1"
            android:textColor="#FFFFFF" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:background="#654321"
            android:gravity="center"
            android:text="weight = 2"
            android:textColor="#FFFFFF" />
    </LinearLayout>

</LinearLayout>

抱歉!评论已关闭.