Android - 无法将视图设置为 match_parent 卡内回收器视图的高度

Android - Can't set view to match_parent height inside card for recycler view

我有一个回收器视图,它由 card_view 提供,左边缘有一个颜色条(视图),应该占据卡片的整个高度。

我尝试将颜色视图的高度设置为 match_parent,但如果我这样做,颜色永远不会出现。如果我改为为高度分配一个数字,那么它就可以工作,但我需要视图调整到卡片的高度。

这里是 XML 用于回收站视图的卡片:

<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<androidx.cardview.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" >


    <RelativeLayout

        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingEnd="14dp"
         >

        <View
            android:id="@+id/swatch"
            android:layout_width="12dp"
            android:layout_height="match_parent" />

        <TextView
            android:id="@+id/pathTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_toEndOf="@id/swatch"
            android:layout_marginStart="8dp"
            android:layout_marginTop="14dp"
            android:padding="2dp"
            android:text="@string/placeholder_title"
            android:textColor="@android:color/black"
            android:textSize="14sp" />

        <TextView
            android:id="@+id/pathDescription"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/pathTitle"
            android:layout_toEndOf="@id/swatch"
            android:layout_marginStart="8dp"
            android:layout_marginBottom="14dp"
            android:padding="2dp"
            android:text="@string/placeholder_title"
            android:textColor="@android:color/black"
            android:textSize="14sp" />

    </RelativeLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>

如何让“样本”视图适应卡片的高度?

希望能解决您的问题

<androidx.cardview.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <View
            android:id="@+id/swatch"
            android:layout_width="12dp"
            android:layout_height="match_parent" />
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <TextView
                android:id="@+id/pathTitle"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="8dp"
                android:layout_marginTop="14dp"
                android:padding="2dp"
                android:text="@string/placeholder_title"
                android:textColor="@android:color/black"
                android:textSize="14sp" />
            <TextView
                android:id="@+id/pathDescription"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="8dp"
                android:layout_marginBottom="14dp"
                android:padding="2dp"
                android:text="@string/placeholder_title"
                android:textColor="@android:color/black"
                android:textSize="14sp" />
        </LinearLayout>
    </LinearLayout>
</androidx.cardview.widget.CardView>