首页 > 解决方案 > 去除卡片之间的空间

问题描述

我正在使用 RecyclerView 来存储这些卡片,但我想减少卡片之间的空间。添加 cardPreventCornerOverlap="false" 这一行后,它减少了一点,但仍然太多了。有没有办法做到这一点?

卡片布局:

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardCornerRadius="10sp"
    app:cardBackgroundColor="@color/lightest_black"
    app:cardElevation="16dp"
    app:cardUseCompatPadding="true"
    app:cardPreventCornerOverlap="false"
    android:foreground="@drawable/ripple">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/sick_linear_layout"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/sick_topic"
            android:text=""
            android:textSize="22sp"
            android:padding="16dp"
            android:fontFamily="@font/productsansbold"
            android:textColor="@color/white"/>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/sick_expandable_layout">

            <TextView
                android:id="@+id/sick_content"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:fontFamily="@font/productsansregular"
                android:padding="16dp"
                android:text=""
                android:textColor="@color/gray"
                android:textSize="18sp" />

        </RelativeLayout>

    </LinearLayout>

</androidx.cardview.widget.CardView>

RecyclerView 布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    android:background="@color/light_black"
    android:paddingTop="8dp"
    android:paddingRight="8dp"
    android:paddingLeft="8dp"
    tools:context=".IfYoureFeelingSick">

    <androidx.recyclerview.widget.RecyclerView
        android:layout_marginTop="5dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/sick_recyclerView"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"/>

</LinearLayout>

这就是现在的样子

标签: androidpaddingandroid-cardview

解决方案


尝试添加android:layout_margin="-10dp"到您CardView以控制由创建的不需要的空间CompatPadding,无需说您可以将 -10 替换为您想要的任何负数:

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="10sp"
android:layout_margin="-10dp"
.....
>

推荐阅读