简而言之,是否可以让一个位于RelativeLayout
中的子视图始终与该RelativeLayout
的高度匹配,无论该RelativeLayout
中的其他子视图如何运作? 总之,就是这样。详细信息如下。
我要实现的是一个ListView
行,其中至少有三个视图,其中一个视图将成为列表条目右侧的一种条纹(如下面的红色视图)。 我遇到的问题是在左侧有一个TextView
,而取决于我有多少文本,条纹将无法填满整个布局。 这一点通过下面的图片非常清楚地解释了。
ListView
项布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<View
android:id="@+id/bottom_line"
android:layout_width="match_parent"
android:layout_height="5dp"
android:layout_alignParentBottom="true"
android:background="#fc0" />
<!-- Why match_parent does not work in view below? -->
<View
android:id="@+id/stripe"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:minHeight="50dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:background="#f00" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@id/stripe"
android:text="This is a very long line, meant to completely break the match_parent property of the box at right"
style="?android:textAppearanceLarge"/>
</RelativeLayout>
结果:
将条纹和根元素的高度设置为match_parent
没有任何区别。我已经这样做了。
重申一下问题,我希望红色条纹始终在垂直方向上填充父容器。 您可以看到条纹未对齐到根元素的顶部。
注意:上面的示例是我能想到的最简单、自包含的示例。它只是一个由静态字符串数组填充的匿名ArrayAdapter
的ListActivity
。在onCreate
中的相关代码最多8行,所以不用担心。真正的问题出在布局上。此外,我已经使用嵌套的LinearLayout
使其正常工作,但如果可能的话,我想减少布局结构的深度。LinearLayout
完全符合预期。