带有 LayoutWeight 的 LinearLayout 不起作用

LinearLayout with LayoutWeight not working

我有以下布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/color_brand"
                android:weightSum="100">

    <LinearLayout
        android:id="@+id/top"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="40"
        android:background="@color/color_white">

        <View
            android:layout_width="match_parent"
            android:layout_height="10dp"
            />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/middle"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="20"
        android:background="@color/color_black"
        android:layout_below="@id/top">

        <View
            android:layout_width="match_parent"
            android:layout_height="10dp"/>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/bottom"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="40"
        android:background="@color/color_white"
        android:layout_below="@id/middle">

        <View
            android:layout_width="match_parent"
            android:layout_height="10dp"/>

    </LinearLayout>

</RelativeLayout>

我想要布局之间的 40-20-40 分割,我已经尝试了所有方法,但似乎没有任何效果。我试过在线性布局中添加一个空视图,我已经为线性布局中的视图赋予了权重,但没有任何效果。有人可以指出我做错了什么吗?

试试这个

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:background="@color/color_brand">

<LinearLayout
    android:id="@+id/top"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="40"
    android:background="@color/color_white"
   >

    <View
        android:layout_width="match_parent"
        android:layout_height="10dp"
        />

</LinearLayout>

<LinearLayout
    android:id="@+id/middle"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="20"
    android:background="@color/color_black"
    android:layout_below="@id/top"

    >

    <View
        android:layout_width="match_parent"
        android:layout_height="10dp"
       />

</LinearLayout>

<LinearLayout
    android:id="@+id/bottom"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="40"
    android:background="@color/color_white"
    android:layout_below="@id/middle"

   >

    <View
        android:layout_width="match_parent"
        android:layout_height="10dp"
        />

</LinearLayout>

您的 parent 是相对布局,为什么不起作用

将您的主根父级更改为 LinearLayout 并为其设置垂直方向。 RelativeLayout 不支持 weightsum,正如您在代码中看到的那样,您正在为高度定义 0dp,因此您必须使根视图 LinearLayout 具有垂直方向才能使权重起作用。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/color_brand"
            android:weightSum="100">

     --------
</LinearLayout>

WeightSum 仅适用于 LinearLayout。所以你必须改变你的 parent RelativeLayoutLinearLayout.

所以请更改此代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/color_brand"
                android:weightSum="100">

至此

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/color_brand"
                android:weightSum="100"
                android:orientation="vertical">

Note : add orientation in the LinearLayout.

android:weightSum 不是 RelativeLayout 的属性,它是 LinearLayout 的属性。所以你可以将父布局更改为 LinearLayout 或者你可以使用 PercentRelativeLayout

代码片段

<android.support.percent.PercentRelativeLayout
         xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:app="http://schemas.android.com/apk/res-auto"
         android:layout_width="match_parent"
         android:layout_height="match_parent">
     <ImageView
         app:layout_widthPercent="50%"
         app:layout_heightPercent="50%"
         app:layout_marginTopPercent="25%"
         app:layout_marginLeftPercent="25%"/>
 </android.support.percent.PercentRelativeLayout>

删除您的相对布局或根据您的方向将其更改为线性。它会起作用。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="100">

    <LinearLayout
        android:id="@+id/top"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="40">

        <View
            android:layout_width="match_parent"
            android:layout_height="10dp"
            />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/middle"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="20"
        android:background="@color/colorBlack"
        android:layout_below="@id/top">

        <View
            android:layout_width="match_parent"
            android:layout_height="10dp"/>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/bottom"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="40"
        android:layout_below="@id/middle">

        <View
            android:layout_width="match_parent"
            android:layout_height="10dp"/>

    </LinearLayout>

</LinearLayout>

使用它可以解决您的问题。还有一件事,当您想根据重量管理布局时,您必须使用线性布局,因为重量概念在相对布局中不起作用。

20-40-20 试试这个

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:weightSum="100">

    <LinearLayout
        android:id="@+id/top"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_weight="40"
        android:background="@android:color/darker_gray">

        <View
            android:layout_width="match_parent"
            android:layout_height="10dp" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/middle"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_weight="20"
        android:background="@android:color/black"
        android:layout_below="@id/top">

        <View
            android:layout_width="match_parent"
            android:layout_height="10dp" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/bottom"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_weight="40"
        android:background="@android:color/darker_gray"
        android:layout_below="@id/middle">

        <View
            android:layout_width="match_parent"
            android:layout_height="10dp" />

    </LinearLayout>
</LinearLayout>  

输出:

试试这个 40-20-40

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:weightSum="10">
    <LinearLayout
        android:id="@+id/middle"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_weight="3"
        android:background="@android:color/black"
        android:layout_below="@id/top">

        <View
            android:layout_width="match_parent"
            android:layout_height="10dp" />
    </LinearLayout>
    <LinearLayout
        android:id="@+id/top"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_weight="4"
        android:background="@android:color/darker_gray">

        <View
            android:layout_width="match_parent"
            android:layout_height="10dp" />

    </LinearLayout>


    <LinearLayout
        android:id="@+id/bottom"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_weight="3"
        android:background="@android:color/black"
        android:layout_below="@id/middle">

        <View
            android:layout_width="match_parent"
            android:layout_height="10dp" />

    </LinearLayout>

</LinearLayout>  

输出

您必须将 LinearLayout 作为父级才能使用 weightSum,因为 RelativeLayout 不支持 weightSum。 现在你必须使用 LinearLayout 而不是 RelativeLayout。 你必须像下面这样写你的代码。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="@color/color_brand"
 android:orientation="vertical"
 android:weightSum="100">

    <LinearLayout
        android:id="@+id/top"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="40"
        android:background="@color/color_white">

        <View
            android:layout_width="match_parent"
            android:layout_height="10dp" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/middle"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_below="@id/top"
        android:layout_weight="20"
        android:background="@color/color_black">

        <View
            android:layout_width="match_parent"
            android:layout_height="10dp" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/bottom"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_below="@id/middle"
        android:layout_weight="40"
        android:background="@color/color_white">

        <View
            android:layout_width="match_parent"
            android:layout_height="10dp" />

    </LinearLayout>


</LinearLayout>