垂直回收器视图中的列未居中

Posted

技术标签:

【中文标题】垂直回收器视图中的列未居中【英文标题】:Columns in the vertical recycler view are not centered 【发布时间】:2020-06-17 05:19:57 【问题描述】:

左右,我的柱子和屏幕边缘之间的距离不同。但我想看到他们一样。

当前状态

Item.xml

<com.google.android.material.card.MaterialCardView
    android:layout_
    android:layout_>
    ...
</com.google.android.material.card.MaterialCardView>

Layout.xml

</LinearLayout>
     android:layout_
     android:layout_>
     ...
      <androidx.recyclerview.widget.RecyclerView
            android:layout_
            android:layout_
            app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
            app:spanCount="4"
            .../>
</LinearLayout>

【问题讨论】:

【参考方案1】:

我通过添加一个 match_parent 宽度的根元素并设置嵌入视图的 center_horizo​​ntal 解决了这个问题。

Item.xml

<FrameLayout
    android:layout_
    android:layout_>

    <com.google.android.material.card.MaterialCardView
        android:layout_
        android:layout_
        android:layout_gravity="center_horizontal">
        ...
    </com.google.android.material.card.MaterialCardView>
</FrameLayout>

结果:

【讨论】:

以上是关于垂直回收器视图中的列未居中的主要内容,如果未能解决你的问题,请参考以下文章

水平和垂直回收器视图中的滚动问题

Vertical RecyclerView 中的 Horizo​​ntal RecyclerViews 丢失位置/状态

在尝试长时间无法在水平和垂直的回收器视图内的 2 个卡片视图元素之间提供空间 [重复]

使用 IOS 中的约束将 UIView 中的子视图居中

在 IOS 中使用自动布局在邻居之间居中视图

表格中的文字怎样垂直居中