【问题标题】:Margin between items in recycler view Android回收站视图Android中项目之间的边距
【发布时间】:2016-09-27 05:41:27
【问题描述】:

您好,我正在学习本教程:

http://www.journaldev.com/10024/android-recyclerview-and-cardview-example-tutorial

现在我面临一个奇怪的问题,RecyclerView 内每个 CardView 项目之间的边距太大了。

问题

如何减少CardView 放在RecyclerView 内的每一项之间的边距?

【问题讨论】:

    标签: android android-recyclerview android-cardview


    【解决方案1】:

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

    要解决此问题,请找到包含每一行的 xml 文件,并确保根元素的高度是 wrap_content 而不是 match_parent

    【讨论】:

    • 是的,这行得通,必须删除父级相对布局匹配父级来包装内容
    • 如果您有相对布局的“fill_parent”并将其更改为“wrap_content”,也可以使用。完美运行。太好了!
    • 仅适用于图像,wrap_content 不起作用。@maher
    【解决方案2】:

    我开设了一个课程来解决这个问题。该类为 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;
            }
        }
    }
    

    您可以通过这种方式在您的回收站视图中设置它

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

    【讨论】:

    • @Victor 我有问题的答案,如果我更新 item 的位置呢?它应该令人耳目一新,但事实并非如此。并且在第一行的底部的项目仍然有边距
    【解决方案3】:
    1. cards_layout.xml中找到属性card_view:cardUseCompatPadding="true"并删除。启动app,你会发现每个cardview item之间没有margin。

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

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

    【讨论】:

    • 谢谢,为我工作,虽然这是 card_view:cardUseCompatPadding="true" 没有完成工作的唯一一种情况
    【解决方案4】:

    在 RecyclerView 中的 item 之间添加边距不是使用 XML,而是使用 android 框架提供的 RecyclerView.ItemDecoration 更好的方法。

    所以,我创建了一个库来解决这个问题。

    https://github.com/TheKhaeng/recycler-view-margin-decoration

    【讨论】:

    • 你制作了一个很棒的图书馆。谢谢!你救了我的一天! :)
    • 简单好用的库
    • 我会推荐 SpacingItemDecoration 库。这是最好的
    【解决方案5】:

    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">
    

    【讨论】:

      【解决方案6】:

      在 CardView 项中使用 CompatPadding

      【讨论】:

        【解决方案7】:

        如果您想在 XML 中执行此操作,只需将 paddingToppaddingLeft 设置为您的 RecyclerView 并将等量的 layoutMarginBottomlayoutMarginRight 设置为您膨胀到的项目你的RecyclerView(反之亦然)。

        【讨论】:

          【解决方案8】:
          【解决方案9】:

          在 XML 中,为了管理 RecyclerView 的项目之间的边距,我是这样做的, 对细节布局的这些属性进行操作(我使用的是RelativeLayout):

          • android:layout_marginTop
          • android:layout_marginBottom

          当然设置这些属性也很重要:

          • android:layout_width="wrap_content"
          • android:layout_height="wrap_content"

          下面是一个完整的例子, 这是项目列表的布局:

          <?xml version="1.0" encoding="utf-8"?>
          <androidx.recyclerview.widget.RecyclerView 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:name="blah.ui.meetings.MeetingEventFragment"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:layout_marginLeft="0dp"
          android:layout_marginRight="0dp"
          android:layout_marginVertical="2dp"
          app:layoutManager="LinearLayoutManager"
          tools:context=".ui.meetings.MeetingEventFragment"
          tools:listitem="@layout/fragment_meeting_event" />
          

          这是详细布局

          <?xml version="1.0" encoding="utf-8"?>
          <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginTop="1dp"
          android:layout_marginBottom="1dp"
          android:background="@drawable/background_border_meetings">
              <TextView
              android:id="@+id/slot_type"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignParentStart="true"
              android:text="slot_type"
              />
          </RelativeLayout>
          

          background_border_meetings 只是一个盒子:

          <?xml version="1.0" encoding="utf-8"?>    
          <shape xmlns:android="http://schemas.android.com/apk/res/android"
          android:shape="rectangle" >
              <!-- This is the stroke you want to define -->
              <stroke android:width="4dp"
              android:color="@color/mdtp_line_dark"/>
              <!-- Optional, round your corners -->
              <corners android:bottomLeftRadius="2dp"
              android:topLeftRadius="2dp"
              android:bottomRightRadius="2dp"
              android:topRightRadius="2dp" />
              <gradient android:startColor="@android:color/transparent"
              android:endColor="@android:color/transparent"
              android:angle="90"/>
          </shape>
          

          【讨论】:

            【解决方案10】:

            我遇到了类似的问题,在我的例子中,我使用了负数的布局边距。也许不是最好的主意,但它奏效了。

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

            【讨论】:

            • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
            【解决方案11】:

            将 Recycler 视图 ma​​tch_parent 更改为 wrap_content

            <android.support.v7.widget.RecyclerView
                android:id="@+id/recycleView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
            

            项目布局xml也发生变化

            将父布局高度 ma​​tch_parent 设置为 wrap_content

            <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
            
                <TextView
                    android:id="@+id/textView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                />
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2021-01-12
              • 1970-01-01
              • 1970-01-01
              • 2016-01-24
              • 2017-10-30
              • 1970-01-01
              • 2022-06-14
              • 1970-01-01
              相关资源
              最近更新 更多