【发布时间】: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