带有LayoutWeight的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
        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并使其垂直方向。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>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章