当使用 RecyclerView 用一个 GridLayoutManager ,我的单元格总是左对齐:空闲空间总是出现在单元格的右侧。使用 ItemDecoration 计算偏移只会使单元格变小,从而保持单元格右侧的自由空间。 gravity="center" 在 回收站视图 和 layout_gravity="center" 在牢房里也没用。
RecyclerView
GridLayoutManager
ItemDecoration
gravity="center"
回收站视图
layout_gravity="center"
在我的头重重地撞在墙上之后,发现问题是使用固定大小的电池。通过设置 android:layout_width="match_parent" 在牢房里,情况开始正常 ItemDecoration 正确应用间距。我也删除了 columnWidth="@dimen/[n]dp" ,尽管我还没有测试过这是否必要。
android:layout_width="match_parent"
columnWidth="@dimen/[n]dp"