RecyclerView GridLayoutManager 布局管理器 item 居中问题【完美】

🙂 Hello,村长

布局效果对比


在这里插入图片描述

在这里插入图片描述

解决

不居中的原因,我的 item 布局根容器使用的是 ConstraintLayout

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

布局根容器使用 LinearLayout即可,必须同时指定 android:layout_width="match_parent" 和 android:gravity="center"

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"

我的上述布局,根布局容器 LinearLayout 只有一个子控件,就是 ConstraintLayout,如有差异,请调整自己的子控件。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值