首页 > 解决方案 > 如何减少 RecyclerView 中各个 CardView 之间的空间?

问题描述

所以我正在学习实现 RecyclerViews 和 CardViews。我发现这些单独的 CardViews 之间有很多空间。这是 RecyclerView 和 CardView 的 XML 代码。

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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="match_parent"
    tools:context=".MainActivity"
    android:background="@android:color/transparent">

    <TextView
        android:id="@+id/bannerId"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_marginTop="10dp"
        android:text="@string/favorite_artists_text"
        android:textSize="27sp"
        android:textAlignment="center"
        android:fontFamily="@font/montserrat_alternates_bold"/>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/RecyclerView"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        app:layout_constraintTop_toBottomOf="@id/bannerId"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:background="@android:color/transparent"
        android:layout_marginTop="65dp"/>

</androidx.constraintlayout.widget.ConstraintLayout>

layout_listitems.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/primaryCardView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    app:cardMaxElevation="1dp"
    app:cardElevation="1dp">

    <RelativeLayout
        android:id="@+id/housingRelativeLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="3dp">

        <TextView
            android:id="@+id/rank"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="@font/old_standard_tt_bold"
            android:text="@string/sample_rank"
            android:textAlignment="center"
            android:textSize="27sp" />

        <View
            android:id="@+id/separator_line"
            style="@style/separator_line"
            android:layout_below="@id/rank"/>

        <TextView
            android:id="@+id/songName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/separator_line"
            android:layout_marginTop="15dp"
            android:text="@string/sample_song_name"
            android:textSize="17sp"
            android:fontFamily="@font/varela_round"/>

        <TextView
            android:id="@+id/artistName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/songName"
            android:text="@string/sample_artist_name"
            android:textSize="17sp"
            android:fontFamily="@font/varela_round"/>

    </RelativeLayout>

</androidx.cardview.widget.CardView>

我在 Stack Overflow 中查看了其他一些答案,其中一个说添加了这些行:

app:cardMaxElevation="1dp"
app:cardElevation="1dp"

会解决这个问题。但是,它什么也没做。

标签: androidxmlandroid-recyclerviewcardview

解决方案


因为您正在使用android:layout_margin="10dp"cardView意味着您将在10dp所有侧面添加空间,第一个项目现在有10dp顶部和底部,当添加第二个项目时recyclerView将在顶部添加另一个10dp,所以现在第二个项目有20dp边距空间。

怎么修?

您只能使用顶部、开始和结束来设置边距。


推荐阅读