首页 > 解决方案 > Android - 无法将视图设置为卡片内的 match_parent 高度以进行回收器视图

问题描述

我有一个回收站视图,它由一个 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>

如何让“样本”视图调整到卡片的高度?

标签: androidandroid-cardview

解决方案


希望它能解决你的问题

<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>

推荐阅读