【问题标题】:RecyclerView with 2 columns. How set space between them? [duplicate]具有 2 列的 RecyclerView。它们之间如何设置空间? [复制]
【发布时间】:2018-05-08 16:06:33
【问题描述】:

片段集recyclerView列数:

sortRecyclerView.setHasFixedSize(true);
        RecyclerView.LayoutManager rvLayoutManager = new GridLayoutManager(getActivity(), 2);
        sortRecyclerView.setLayoutManager(rvLayoutManager);

这里布局那个内容recyclerView:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/sortRecyclerView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:scrollbars="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

recyclerView中这里的item布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="wrap_content"
    android:layout_marginTop="20dp"
    android:background="@color/place_holder_color">

    <ImageView
        android:id="@+id/imageViewPhoto"
        android:layout_width="@dimen/preview_image_height"
        android:layout_height="@dimen/preview_image_height"
        android:scaleType="centerCrop"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />



</android.support.constraint.ConstraintLayout>

结果如下:

我需要下一个:

  1. 左右两侧设置20dp
  2. 在 2 列之间设置 10 dp

【问题讨论】:

    标签: android


    【解决方案1】:

    你可以这样设置: - 对于第一个 ImageView:

    android:paddingLeft="10dp"
    android:paddingRight="5dp"
    
    • 对于第二个 ImageView:

      android:paddingLeft="5dp"

      android:paddingRight="10dp"

    【讨论】:

    • 没有第二张图片。我更新我的帖子。这是第二列。
    • 我不明白@Alex
    • 我有 2 列的 recyclerView。在每一列上只显示一个 ImageView "@+id/imageViewPhoto"
    • 啊,上面是您用于物品的代码,对吗?
    • 是的,没错
    【解决方案2】:

    添加到左边的一个:

    android:marginRight="5dp"

    android:marginLeft="20dp"

    添加到右边:

    android:marginLeft="5dp"

    android:marginRight="20dp"

    【讨论】:

    • 没有两张图片。我有 2 列的 recyclerView。在每一列上只显示一个 ImageView "@+id/imageViewPhoto"
    • 将该代码添加到父布局中,我建议您使用Relative layout
    • 我找到了解决方案。我必须扩展 RecyclerView.ItemDecoration 并覆盖方法 getItemOffsets()
    猜你喜欢
    • 2017-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-03
    • 1970-01-01
    • 2021-01-31
    相关资源
    最近更新 更多