【问题标题】:How to center content of GridLayoutManager?如何使 GridLayoutManager 的内容居中?
【发布时间】:2020-12-29 12:18:17
【问题描述】:

只是想将我的 RecyclerView 水平居中。

这里是xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">



    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent" 
        android:layout_height="match_parent"/>


</RelativeLayout>

这里是java代码:

mRecyclerView = findViewById(R.id.recycler_view);
itemLayoutManager = new GridLayoutManager(this, 6);
mRecyclerView.setLayoutManager(itemLayoutManager);
...
mItemAdapter = new ItemAdapter(MainActivity.this, mItemList);
mRecyclerView.setAdapter(mItemAdapter);

当我运行应用程序时,recyclerview 左对齐。 我到底要怎么做才能使内容居中,以使边距与右侧示例图片中的左右相同?

【问题讨论】:

  • 确保您的项目宽度为match_partent,并将android:layout_centerInParent="true" 添加到您的RecyclerView
  • 将项目的宽度设置为 match_parent 有效,谢谢 :) 很奇怪,因为在我的旧项目中它的工作方式与我所做的完全一样,但现在我必须改变一些东西。

标签: java android xml android-recyclerview grid-layout


【解决方案1】:

创建一个类,例如为项目设置填充:

class SpacingItemDecorator (private val padding: Int) : RecyclerView.ItemDecoration()
{
    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
    )
    {
        super.getItemOffsets(outRect, view, parent, state)
        outRect.top = padding
        outRect.bottom = padding
        outRect.left = padding
        outRect.right = padding
    }
}

现在在您的 RecyclerView 中设置 paddingStartpaddingEnd,例如 4dp

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingStart="4dp"
    android:paddingEnd="4dp"
    app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
    app:spanCount="6"/>

当您创建适配器时,只需应用此项目装饰器:

val x = (resources.displayMetrics.density * 4).toInt() //converting dp to pixels
recyclerView.addItemDecoration(SpacingItemDecorator(x)) //setting space between items in RecyclerView

RecyclerView 项目:

<androidx.cardview.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardCornerRadius="6dp"
    app:cardElevation="4dp">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/txtContent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{Integer.toString(dataView.id)}"
            android:textSize="20sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>

    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>

结果:

【讨论】:

    猜你喜欢
    • 2013-08-03
    • 1970-01-01
    • 2021-06-18
    • 2015-06-13
    • 2016-12-02
    • 2012-02-17
    • 2013-06-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多