我有以下布局:
<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/