android - 带有 LayoutWeight 的 LinearLayout 不起作用

标签 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"
                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 分割,我已经尝试了所有方法,但似乎没有任何效果。我试过在线性布局中添加一个空 View ,我已经为线性布局中的 View 赋予了权重,但没有任何效果。有人可以指出我做错了什么吗?

最佳答案

将您的主根父级更改为 LinearLayout 并为其设置垂直方向。 RelativeLayout 不支持 weightsum,正如您在代码中看到的那样,您正在为高度定义 0dp,因此您必须使 Root View 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>

关于android - 带有 LayoutWeight 的 LinearLayout 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41235523/

相关文章:

android - 可选查询参数不适用于 Room

PHP 和移动网站

java - Gradle:如何从 JScience jar 依赖项中排除 javax.realtime 包(多个 dex 定义)

java - 动态添加 ScrollView 到线性布局

java - 如何在 LinearLayout 中并排设置 4 个编辑文本?

android - android 中的圆角菜单

java - 如何计算在 TextView 中看到的字母数量?

android - 如何将 ListView 按钮拉伸(stretch)到屏幕的全宽

android - 应用程序被 Android 清除

android - 如何在运行时检测布局的横向版本的存在?