如何减少RecyclerView中各个CardView之间的空间?

问题描述 投票:0回答:1

所以我在学习实现RecyclerViews的同时,也在学习CardViews。我发现这些单个CardView之间有很多空间。下面是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个回答
2
投票

因为你使用的是 android:layout_margin="10dp" 对于 cardView 这意味着你将添加 10dp 四面八方的空间,第一项现在已经 10dp 顶部& 底部,当添加第二项为 recyclerView 再加 10dp 所以,现在第二个项目有 20dp margin space.

如何解决?

你可以只用顶部、开始和结束来设置页边距。


1
投票

就像Moustafa EL-Saghier的解决方案一样,在你的XML中加入这行代码

android:layout_margin="10dp"

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

對於水平的RecyclerView,您可以使用以下指令:在左邊和右邊的卡片之間放上10dp的空間。

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

这将会在左右两边的卡片之间留出空间。

對於垂直的RecyclerView,您可以放:這將在左邊和右邊的卡片之間放上空間。

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

这将会在上下两边的卡片之间留出空间。

同样,这就像Moustafa EL-Saghier的答案一样,只是用简单的代码位来解决你的Cardview需求。希望这能帮到你

© www.soinside.com 2019 - 2024. All rights reserved.