带有CardView的Android RecyclerView:卡之间的边距

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

我目前正在构建我的第一个Android应用,并且正在使用带有Material Design卡的RecyclerView。但是,我希望在卡与屏幕边缘之间以及卡之间具有相同的边距。根据我设置边距的方式,我会遇到不同的问题:

变体1

<com.google.android.material.card.MaterialCardView
    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"
    android:layout_marginTop="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginBottom="8dp"
    app:cardElevation="2dp"
    app:cardUseCompatPadding="false">

...

</com.google.android.material.card.MaterialCardView>

[这导致卡之间的空间比卡和屏幕边缘之间的空间大两倍。

变体2

android:layout_marginTop="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="0dp"

这是我当前的实现。它可以固定两张卡之间的较大空间,但会以列表底部没有空白为代价。即如果我一直滚动到底部,则卡的下边缘与屏幕的下边缘对齐。我也想在那里留一点余地。

变体3:我尝试的第三个选项是将RecyclerView放在额外的布局中,并使用该布局将边距设置为屏幕边缘。这有一个问题,如果您一直滚动到列表的顶部或底部并拉远,则出现的波浪形“过度滚动区域”只会出现在RecyclerView中,而不会延伸到屏幕边缘。因此,它看起来也有点hacky。For clarification I attached a screenshot using a 4dp padding on the RecyclerView

感谢您的帮助;)

android android-recyclerview margins cardview
2个回答
1
投票

仅不要同时添加两者

android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"

这将复制值

添加marginBottom,就足够了

android:layout_marginBottom="8dp"

1
投票

尝试一下。要在卡片视图中应用这些

android:layout_marginTop="4dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="4dp"

并在recyclerview中添加填充

android:paddingTop="4dp"
android:paddingBottom="4dp"
© www.soinside.com 2019 - 2024. All rights reserved.