简体   繁体   English

如何在recyclerview中居中gridlayout

[英]how to center gridlayout in recyclerview

i am trying to implement recyler view with grid layout but i have problem to center the items, the problem is with the items with the small text i try to center all here is my xml我正在尝试使用网格布局实现 recyler 视图,但我无法将项目居中,问题在于我尝试居中的小文本项目是我的 xml

here is 2 picture of the recyler view one is good and one is not这是 2 张回收者视图的图片,一张好,一张不好

https://ibb.co/wMtX1Yr https://ibb.co/8gHNmZT https://ibb.co/wMtX1Yr https://ibb.co/8gHNmZT

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"

    android:gravity="center_horizontal|top"
    android:layout_gravity="center_horizontal|top">

     <ImageView
        android:id="@+id/file_img"
        android:background="@drawable/ic_file"
        android:layout_gravity="center"
        android:gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <TextView
        android:layout_gravity="center"
        android:gravity="center|top"
        android:id="@+id/file_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="10dp"
        android:ellipsize="end"
        android:maxLines="2"/>

</LinearLayout>

I faced this problem before, and I solve it by making the View holder width android:layout_width="match_parent" .我之前遇到过这个问题,我通过使 View holder width android:layout_width="match_parent"来解决它。
and I set spanCount in GridLayoutManager to 2 or whatever you want.我将GridLayoutManager中的 spanCount 设置为 2 或任何你想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM