简而言之,是否可以告诉 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
没有任何区别。我做到了。 重复这个问题,我希望红色条纹始终垂直填充父级。您可以看到条带未对齐到根的顶部。
注意:上面的例子是我能想到的最简单,最独立的例子。这只是ListActivity
,其中匿名ArrayAdapter
由静态String
数组填充。 onCreate
中的相关代码最多为8行,因此不用担心。这真的是布局。此外,我已经使用嵌套的LinearLayout
,但是如果可能的话,我正试图稍微减少布局结构的深度。 LinearLayout
可以正常工作。
答案 0 :(得分:34)
我有同样的要求,我的解决方案是将红色条纹的顶部与父级的顶部对齐,并将条带的底部对齐到左侧文本视图的底部。在这种情况下,高度变得无关紧要。您可以使用wrap_content
或match_parent
。
答案 1 :(得分:20)
只需将主RelativeLayout
放在LinearLayout
内,然后计算子视图高度即可。我有同样的问题,它对我有用。
答案 2 :(得分:6)
试试这个:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<View
android:id="@+id/stripe"
android:layout_width="80dp"
android:layout_height="match_parent"
android:layout_alignBottom="@id/text"
android:layout_alignParentRight="true"
android:layout_alignTop="@id/text"
android:background="#f00"
android:minHeight="50dp"/>
<TextView
android:id="@+id/text"
style="?android:textAppearanceLarge"
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"/>
<View
android:id="@+id/bottom_line"
android:layout_width="match_parent"
android:layout_height="5dp"
android:layout_below="@id/text"
android:background="#fc0"/>
</RelativeLayout>
红色视图的顶部和底部对齐现在与textview相同。
答案 3 :(得分:1)
这是一个恼人的问题,您需要将RelativeLayout放在LinearLayout中
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minHeight="40dp"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/deleteItem"
android:layout_width="60dp"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:clickable="true"
android:background="@color/background_grey"
>
<TextView
style="@style/SmallButtonFont"
android:layout_marginRight="12dp"
android:layout_centerInParent="true"
android:text="CLEAR"
android:textColor="@color/globalRedLight" />
</RelativeLayout>
</RelativeLayout>
</LinearLayout>
答案 4 :(得分:0)
将监听器添加到列表项目持有者,以便每次持有者更改高度时,将该高度设置为子级。 i1
代表top,i3
代表top。
parent.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View view, int i, int i1, int i2, int i3, int i4, int i5, int i6, int i7) {
child.setTop(i1);
child.setBottom(i3);
}
});