简体   繁体   English

Android RecyclerView with CardView:卡片之间的边距

[英]Android RecyclerView with CardView: Margins between cards

I am currently building my first Android app and I am using a RecyclerView with Cards from Material Design.我目前正在构建我的第一个 Android 应用程序,并且我正在使用带有 Material Design 卡片的 RecyclerView。 However, I would like to have the same margins between the cards and the edge of the screen and between the cards.但是,我希望卡片和屏幕边缘以及卡片之间的边距相同。 Depending on how I set the margins, I get different problems:根据我设置边距的方式,我会遇到不同的问题:

Variant 1 :变体 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>

This leads to twice as much space between the cards than between the cards and the edge of the screen.这导致卡片之间的空间是卡片与屏幕边缘之间的两倍。

Variant 2 :变体 2

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

This is my current implementation.这是我目前的实现。 It fixes the wider space between the cards but at the cost of having no margin at the bottom of the list.它修复了卡片之间更宽的空间,但代价是列表底部没有边距。 Ie if I scroll all the way to the bottom, the lower edge of the card lines up with the lower edge of the screen.即,如果我一直滚动到底部,则卡片的下边缘与屏幕的下边缘对齐。 I would also like to have a margin there.我也想在那里有一个利润。

Variant 3 : The third option I tried was putting the RecyclerView inside an extra layout and used the layout to set the margins to the edges of the screen.变体 3 :我尝试的第三个选项是将 RecyclerView 放入一个额外的布局中,并使用该布局将边距设置为屏幕边缘。 This has the problem that if you scroll all the way to the top or bottom of the list and pull further, the appearing wavy "overscroll areas" do only appear within the RecyclerView and do not extend up to the edges of the screen.这有一个问题,如果您一直滚动到列表的顶部或底部并拉得更远,出现的波浪状“过度滚动区域”只会出现在 RecyclerView 中,并且不会延伸到屏幕边缘。 So it also looks a bit hacky.所以它看起来也有点hacky。 For clarification I attached a screenshot using a 4dp padding on the RecyclerView为澄清起见,我在 RecyclerView 上使用 4dp 填充附加了一个屏幕截图

Thanks for helping me out;)谢谢你的协助;)

Try this.To card view layout apply these试试这个。在卡片视图布局中应用这些

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

And to recyclerview add padding并为 recyclerview 添加填充

android:paddingTop="4dp"
android:paddingBottom="4dp"

Just don't add both只是不要同时添加

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

This will duplicate the values这将复制值

Add the marginBottom, it will be sufficient添加marginBottom就足够了

android:layout_marginBottom="8dp"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM