基于下图,我对布局问题感到很生气
该图像代表RelativeLayout.我需要将蓝色视图与黑色视图的底部对齐.但是,如果黑色视图比红色视图和蓝色视图的总和短,我需要蓝色视图低于红色视图.我希望得到这样的结果:
我尝试使用以下xml:
<RelativeLayout android:id="@+id/container" android:layout_width="wrap_content" android:layout_height="wrap_content"> <View android:id="@+id/blackView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:align_parentTop="true" android:align_parentLeft="true"/> <View android:id="@+id/redView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:align_parentTop="true" android:align_parentRight="true" android:layout_toRightOf="@+id/blackView"/> <View android:id="@+id/blueView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/blackView" android:layout_below="@+id/redView" android:layout_alignParentRight="true"/> </RelativeLayout>
但似乎layout_alignBottom的优先级高于layout_below.
我还尝试将blueView设置为与其父级的底部对齐,但结果是父级(具有wrap_content高度)变为高级作为其自己的父级(整个屏幕高度).
有没有人有同样的问题?
解决方法
用这个.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:id="@+id/container" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <View android:id="@+id/blackView" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="0.5" android:background="#000000" /> <RelativeLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="0.5" > <View android:id="@+id/redView" android:layout_width="match_parent" android:layout_height="60dp" android:layout_alignParentTop="true" android:background="#FF0000" /> <View android:id="@+id/blueView" android:layout_width="match_parent" android:layout_height="60dp" android:layout_alignParentBottom="true" android:background="#003CFF" /> </RelativeLayout> </LinearLayout> </LinearLayout>