我有以下布局:
<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不支持权重,正如您在代码中看到的那样,您为高度定义了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>