Android 嵌套视图强制它';s的父母';s大小以匹配\u父项而不是换行\u内容

Android 嵌套视图强制它';s的父母';s大小以匹配\u父项而不是换行\u内容,android,android-layout,Android,Android Layout,我想知道,当父级具有布局\u height=“wrap\u content”时,是否可以让一个视图将其高度调整为父级高度。在以下示例中: 嵌套的ImageView使RelativeLayout扩展其高度以匹配其自身的父级。我想实现的是,RelativeLayout的高度等于内部TextView的高度,ImageView应该在这里作为背景,仅在文本后面延伸。在没有Java代码技巧的纯XML中是否可能实现这一点?如果您使用RelativeLayout(如您所做),则视图或视图组布局可以按id引

我想知道,当父级具有
布局\u height=“wrap\u content”
时,是否可以让一个
视图将其高度调整为父级高度。在以下示例中:



嵌套的
ImageView
使
RelativeLayout
扩展其高度以匹配其自身的父级。我想实现的是,
RelativeLayout
的高度等于内部
TextView
的高度,
ImageView
应该在这里作为背景,仅在文本后面延伸。在没有Java代码技巧的纯XML中是否可能实现这一点?

如果您使用
RelativeLayout
(如您所做),则视图或视图组布局可以按id引用其他视图或视图组。 在您的情况下,如果为TextView
android:ud=“@+id/tvHello”
分配了一个id,则可以将ImageView的顶部和底部与该TextView对齐:

android:layout_alignTop="@+id/tvHello"
android:layout_alignBottom="@+id/tvHello"
为完整起见,以下是您的布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/red"
            android:orientation="vertical">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_alignTop="@+id/tvHello"
        android:layout_alignBottom="@+id/tvHello"
        android:src="@drawable/time"/>
    <TextView
        android:id="@+id/tvHello"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hello"/>
</RelativeLayout>
我希望这能帮你一点忙,而且很有帮助(这是我第一次回答stackoverflow)


快乐编码

谢谢Martijn,这正是我需要的。祝stackoverflow好运:)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/red"
            android:orientation="vertical">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_alignTop="@+id/tvHello"
        android:layout_alignBottom="@+id/tvHello"
        android:src="@drawable/time"/>
    <TextView
        android:id="@+id/tvHello"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hello"/>
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:background="@drawable/time"
     android:orientation="vertical">