我有一个使用GridLayoutManager实现的RecyclerView。
根据数据集中的项目数量,spanCount在1-4之间。项目的宽度取决于spanCount。如果spanCount为4或以上,则保持为4。
如果我有5个项目,这将留下1个项目(一行4个项目,1个项目剩余),位于屏幕左侧,独自占据一行。我希望它居中。
我尝试通过编程方式设置剩余项目的边距,并将recyclerView和单个项目都包装在LinearLayout中,并将重力设置为center,以及设置recyclerView的重力。
示例项目XML:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal"
>
<RelativeLayout
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center"
android:id="@+id/relative"
/>
</LinearLayout>
示例 RecyclerView XML
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal"
>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
有人可以提供解决方案或者给我一些参考资料吗?
先在这里谢谢了。