android - 如何减少 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"
会解决这个问题。但是,它什么也没做。
解决方案
因为您正在使用android:layout_margin="10dp"
这cardView
意味着您将在10dp
所有侧面添加空间,第一个项目现在有10dp
顶部和底部,当添加第二个项目时recyclerView
将在顶部添加另一个10dp
,所以现在第二个项目有20dp
边距空间。
怎么修?
您只能使用顶部、开始和结束来设置边距。
推荐阅读
- python - 如何使用熊猫数据框计算点之间的距离?
- javascript - 让 Discord 机器人命令不区分大小写?
- elasticsearch - 如何在 Elasticsearch 中存储商店产品
- sql - 如何在组合查询中编辑字段
- google-app-maker - 如何在谷歌应用程序制造商中使用@expr 数据进行绑定转换器?
- vb.net - 如何将对象传递给默认表单实例
- java - 来自 ExpandableListView 的子元素中的 setOnClickListener
- r - 根据用户输入日期添加新的计算列
- jenkins - Jenkins 多分支与不同仓库中的 Jenkinsfile
- tomcat - Web 应用程序 [gestionMines] 似乎已经启动了一个名为 [Abandoned connection cleanup thread] 的线程,但未能停止它