Android中Recycler View项目之间的边距

74
12个回答

115

我遇到了类似的问题,每行在recyclerview中使用RelativeLayout作为根元素。

为解决此问题,请找到保存每行的xml文件,并确保根元素的高度为wrap_content而非match_parent


1
是的,这个方法可行。必须将父相对布局的match_parent属性移除,改为wrap_content。 - Tushar Narang
如果您在相对布局中使用了“fill_parent”并将其更改为“wrap_content”,则也可以正常工作。完美运行。太好了! - Benjamin Heinke
仅适用于图像,wrap_content 不起作用。@maher - Nimisha V

53

我创建了一个类来管理这个问题。这个类为recyclerView内部的项目设置不同的边距:只有第一行会有顶部边距,只有第一列会有左侧边距。

public class RecyclerViewMargin extends RecyclerView.ItemDecoration {
private final int columns;
private int margin;

/**
 * constructor
 * @param margin desirable margin size in px between the views in the recyclerView
 * @param columns number of columns of the RecyclerView
 */
public RecyclerViewMargin(@IntRange(from=0)int margin ,@IntRange(from=0) int columns ) {
    this.margin = margin;
    this.columns=columns;

}

/**
 * Set different margins for the items inside the recyclerView: no top margin for the first row
 * and no left margin for the first column.
 */
@Override
public void getItemOffsets(Rect outRect, View view,
                           RecyclerView parent, RecyclerView.State state) {

    int position = parent.getChildLayoutPosition(view);
    //set right margin to all
    outRect.right = margin;
    //set bottom margin to all
    outRect.bottom = margin;
    //we only add top margin to the first row
    if (position <columns) {
        outRect.top = margin;
    }
    //add left margin only to the first column
    if(position%columns==0){
        outRect.left = margin;
        }
    }
}

你可以通过以下方式在你的RecyclerView中进行设置

 RecyclerViewMargin decoration = new RecyclerViewMargin(itemMargin, numColumns);
 recyclerView.addItemDecoration(decoration);

@Victor,我有一个问题,如果我更新了项目的位置,它应该刷新,但实际上没有。而且在底部的项目进入第一行后仍然会有边距。 - jboxxpradhana

45
  1. cards_layout.xml 中找到属性 card_view:cardUseCompatPadding="true" 并删除它。启动应用程序,您会发现每个卡片视图项目之间没有边距。

  2. 添加您喜欢的边距属性。例如:

  3. android:layout_marginTop="5dp"
    android:layout_marginBottom="5dp" 
    

1
谢谢,对我有用,尽管这只是一个例外情况,card_view:cardUseCompatPadding="true" 没有起作用。 - Amit Shadadi

12

你制作了一个很棒的库。谢谢!你救了我的一天! :) - Ahad
简单而强大的库 - i.am.jabi
我会推荐使用 SpacingItemDecoration 库。这是最好的选择。 - Vlad

10

像这样在Recyclerview的项目布局中使用CardView

 <android.support.v7.widget.CardView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:card_view="http://schemas.android.com/tools"
        card_view:cardCornerRadius="10dp"
        app:cardBackgroundColor="#ACACAC"
        card_view:cardElevation="5dp"
        app:contentPadding="10dp"
        card_view:cardUseCompatPadding="true">

7

在CardView项目中使用CompatPadding


2

如果您想在 XML 中实现此操作,只需将 paddingToppaddingLeft 设置为您的 RecyclerView 并将等量的 layoutMarginBottomlayoutMarginRight 设置为您填充到 RecyclerView 中的项目(或反之亦然)。


2

1

为了在RecyclerView中的两个项目之间添加空间,我只需添加marginTopmarginBottom即可,这样就可以正常工作。

<androidx.cardview.widget.CardView
    android:layout_marginTop="10dp"
    android:layout_marginBottom="10dp"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:elevation="15dp"
    app:cardCornerRadius="10dp">

0

我之前遇到过类似的问题,我的解决方案是将布局边距设为负值。也许不是最好的做法,但它确实奏效了。

android:layout_marginStart="-8dp"
android:layout_marginEnd="-8dp"

你的答案可以通过提供额外的支持信息来改进。请[编辑]以添加更多细节,例如引用或文档,以便其他人可以确认你的答案是正确的。您可以在帮助中心找到有关如何编写好的答案的更多信息。 - Community

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接