Android 相对视图,将一个视图与另一个视图的底部对齐,但始终位于另一个视图的下方
根据下图,我对一个布局问题非常恼火 该图像表示相对位置。我需要蓝色视图与黑色视图的底部对齐。但是,如果黑色视图短于红色视图和蓝色视图之和,我需要蓝色视图在红色视图之下。我想得到这个结果: 我尝试使用以下xml:Android 相对视图,将一个视图与另一个视图的底部对齐,但始终位于另一个视图的下方,android,android-relativelayout,Android,Android Relativelayout,根据下图,我对一个布局问题非常恼火 该图像表示相对位置。我需要蓝色视图与黑色视图的底部对齐。但是,如果黑色视图短于红色视图和蓝色视图之和,我需要蓝色视图在红色视图之下。我想得到这个结果: 我尝试使用以下xml: <RelativeLayout android:id="@+id/container" android:layout_width="wrap_content" android:layout_height="wrap_content"> &l
<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>
但看起来底部的布局比下面的布局优先级更高。
我还尝试将blueView与其父级的底部对齐,但结果是父级(具有wrap\u内容
height)变高为其自己的父级(整个屏幕高度)
有人有同样的问题吗?试试这个
<LinearLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="200dp" >
<View
android:id="@+id/blackView"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.4"
android:background="#000000" />
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.6" >
<View
android:id="@+id/redView"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:background="#FF0000" />
<View
android:id="@+id/blueView"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:background="#003CFF" />
</RelativeLayout>
</LinearLayout>
使用此选项
<?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>
已解决
<LinearLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="100dp"
android:orientation="horizontal" >
<View
android:id="@+id/blackView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<View
android:id="@+id/redView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom|right" >
<View
android:id="@+id/blueView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
诀窍在于
android:gravity=“bottom | right”
。谢谢你的帮助 我可以通过使用零尺寸视图标记位置来解决此问题:
<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/blackViewBotom"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_centerHorizontal="true"
android:layout_below="@id/blackView"/>
<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_above="@id/blackViewBotom"
android:layout_alignParentRight="true"/>
相对布局:只需添加android:layout\u alignParentBottom=“true”
此底部视图不需要android:layout_down=“@+id/txtTitle”属性。请发布完整的布局XML。发布了XML的有用部分。这里的问题是容器(LinearLayout)具有固定高度。但是我需要在那里有一个包装内容,因为在该视图下还有其他视图。@Massimo您可以对容器(线性布局)使用
android:layout\u height=“0dp”
weight
,如0.4和剩余视图weight
0.6