在列表中的卡片视图之间更小 Space

Smaller Space Between Card View in a List

我正在尝试缩小列表中卡片视图之间的 space;目前,space 太大了。我在 SO 上到处都看过,但到目前为止似乎还没有一个好的解决方案。我似乎无法找出实现此目的的正确布局:

<?xml version="1.0" encoding="utf-8"?>

<androidx.cardview.widget.CardView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:orientation="vertical"
        android:foreground="?android:attr/selectableItemBackground"
        android:clickable="true"
        android:focusable="true"
        android:layout_width="match_parent"
        android:layout_height="100dp"


        card_view:cardUseCompatPadding="true"
        card_view:cardElevation="5dp"
        card_view:cardCornerRadius="30dp"
        card_view:contentPadding="5dp">


    <RelativeLayout android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:padding="16dp"
                    android:orientation="vertical">

        <TextView
                android:id="@+id/sub_txt"
                android:textSize="18sp"
                android:text="Title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textStyle="italic"
                android:layout_toLeftOf="@+id/img"
                android:layout_below="@+id/txt"
                android:layout_marginStart="20dp"
                />
        <ImageView android:layout_width="30dp"
                   android:layout_height="wrap_content"
                   android:id="@+id/img"
                   android:contentDescription="@string/app_name"/>

        <TextView
                android:id="@+id/txt"
                android:textSize="22sp"
                android:text="Title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textStyle="bold"
                android:layout_toRightOf="@+id/img"
                android:layout_marginStart="20dp"/>

    </RelativeLayout>

</androidx.cardview.widget.CardView>

尽量让你的卡片高度包裹内容

android:layout_height="wrap_content"

CardView 默认情况下会在 Lollipop 之前的平台中添加额外的填充以绘制阴影。在 Lollipop 设备中,您必须设置 cardUseCompatPadding=true,使用下面的行。

card_view:cardUseCompatPadding="true" 

将您的 CardView 身高更改为 wrap_content,然后您可以将 RelativeLayout 身高设置为 match_parent

您已将 100dp 设置为 CardViewRelativeLayout 高度小于导致项目之间显示更多 space 的高度。

尝试删除 cardUseCompatPadding 并添加 margin,如下所示:

<androidx.cardview.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:foreground="?android:attr/selectableItemBackground"
    android:clickable="true"
    android:focusable="true"
    android:layout_width="match_parent"
    android:layout_height="100dp"

    android:layout_marginTop="10dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"

    card_view:cardElevation="5dp"
    card_view:cardCornerRadius="30dp"
    card_view:contentPadding="5dp">