RelativeLayout中子项的match_parent属性

dav*_*ino 26 android android-layout android-relativelayout

简而言之,是否有可能告诉a中的孩子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>
Run Code Online (Sandbox Code Playgroud)

结果:

具有子项的RelativeLayout未垂直填充整个布局

设置条纹和根高度match_parent没有区别.我做的.

重复这个问题,我希望红色条纹始终垂直填充父级.您可以看到条带未对齐到根的顶部.

注意:上面的例子是我能想到的最简单,最独立的例子.它只是一个由静态数组填充ListActivity的匿名.相关代码最多为8行,所以不用担心.这真的是布局.此外,我已经使用嵌套s了,但是如果可能的话,我正试图稍微减少布局结构的深度.按预期正常工作.ArrayAdapterStringonCreateLinearLayoutLinearLayout

ser*_*nka 35

我有同样的要求,我的解决方案是将红色条纹的顶部与父级的顶部对齐,并将条带的底部对齐到左侧文本视图的底部.在这种情况下,高度变得无关紧要.你可以使用wrap_contentmatch_parent.


小智 20

只需将您的主要RelativeLayout内部放入一个LinearLayout,然后计算子视图高度将是正确的.我有同样的问题,它对我有用.


Nam*_*los 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>
Run Code Online (Sandbox Code Playgroud)

红色视图的顶部和底部对齐现在与textview相同.