Android 布局重量不工作的线性布局

Android 布局重量不工作的线性布局,android,android-layout,android-linearlayout,android-layout-weight,Android,Android Layout,Android Linearlayout,Android Layout Weight,我的布局如下: <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"

我的布局如下:

<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>


您的父级是相对布局,这就是为什么不工作的原因

将主根父级更改为
线性布局
,并为其提供垂直方向。RelativeLayout不支持
weightsum
,正如您在代码中看到的,您正在为高度定义
0dp
,因此您必须使根视图以垂直方向线性布局,以使权重正常工作

<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
仅适用于
线性布局
。因此,您必须将您的父项
RelativeLayout
更改为
LinearLayout

所以改变你的密码

<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">

注意:在
线性布局中添加
方向


android:weightSum
不是
RelativeLayout
的属性,而是
LinearLayout
的属性。因此,您可以将父布局更改为
LinearLayout
,也可以使用

代码片段

<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 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>


只需将父布局从RelativeLayout更改为LinearLayout即可。将工作权重作为父项不适用于相对布局,您需要使用线性布局作为父项谢谢各位。我真不敢相信我没有得到。使用线性布局作为父视图。它会起作用。相对布局不支持权重。使用20-40-20组合尝试此
weightSum=100