Android 具有2列的RecyclerView。如何设置它们之间的间距?
片段集recyclerView列数:Android 具有2列的RecyclerView。如何设置它们之间的间距?,android,Android,片段集recyclerView列数: sortRecyclerView.setHasFixedSize(true); RecyclerView.LayoutManager rvLayoutManager = new GridLayoutManager(getActivity(), 2); sortRecyclerView.setLayoutManager(rvLayoutManager); 此处显示内容回收视图的布局: <?xml version="1.0
sortRecyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager rvLayoutManager = new GridLayoutManager(getActivity(), 2);
sortRecyclerView.setLayoutManager(rvLayoutManager);
此处显示内容回收视图的布局:
<?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>
<?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>
我需要下一个:
在左侧和右侧设置20dp
在两列之间设置10 dp
您可以设置如下内容:
-对于第一个ImageView:
android:paddingLeft="10dp"
android:paddingRight="5dp"
- 对于第2个图像视图:
android:paddingLeft="10dp"
android:paddingRight="5dp"
android:paddingLeft=“5dp”
android:paddingRight=“10dp”
添加到左侧的:
android:marginRight=“5dp”
android:marginLeft=“20dp”
添加到右侧的一个:
android:marginLeft=“5dp”
android:marginRight=“20dp”
没有第二张图像。我更新我的帖子。这是第二列。我不明白@AlexI have recyclerView有两列。在每一列上只显示一个ImageView“@+id/imageViewPhoto”啊,上面是您用于项目的代码,对吗?是的,没错没有两个图像。我有两列的recyclerView。在每个列上仅显示一个ImageView“@+id/imageViewPhoto”将该代码添加到父布局中,我建议您使用相对布局
我找到了解决方案。我必须扩展RecyclerView.ItemDecoration并重写方法getItemOffsets()