【问题标题】:How can I reduce the space between individual CardViews in a RecyclerView?如何减少 RecyclerView 中各个 CardView 之间的空间?
【发布时间】:2020-08-10 02:38:40
【问题描述】:

所以我正在学习实现 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"

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

【问题讨论】:

    标签: android xml android-recyclerview cardview


    【解决方案1】:

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

    如何解决?

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

    【讨论】:

    • 知道了。感谢您的帮助。
    • 这是我的荣幸。
    【解决方案2】:

    就像 Moustafa EL-Saghier 的解决方案一样,将这行代码放入您的 XML 中

    android:layout_margin="10dp"
    

    您基本上是在告诉程序在 RecyclerView 中的所有卡片之间放置 10dp 的空间,包括侧面。

    对于一个水平的 RecyclerView,你会想把:

    android:layout_marginStart ="10dp"
    android:layout_marginEnd = "10dp"
    

    这将在左侧和右侧的卡片之间留出空间。

    对于一个垂直的 Recyclerview,你会想把:

    android:layout_marginTop ="10dp"
    android:layout_marginBottom = "10dp"
    

    这将在顶部和底部的卡片之间留出空间。

    同样,这就像 Moustafa EL-Saghier 的回答一样,只是通过简单的代码位来满足您的 Cardview 需求。希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 2017-09-04
      • 2018-11-10
      • 1970-01-01
      • 1970-01-01
      • 2010-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-18
      相关资源
      最近更新 更多