简体   繁体   English

如何将 Recycler View 的项目居中?

[英]How do i center the items of a Recycler View?

i have managed to make a RecyclerView list but am unable to center the text inside.我设法制作了一个 RecyclerView 列表,但无法将文本居中。 Below are the codes:以下是代码:

RecyclerView:回收站视图:

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/list2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:layout_marginEnd="200dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

Item:物品:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    >

    <TextView
        android:id="@+id/listItem"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@font/caviar_dreams"
        android:textSize="24sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:paddingBottom="15dp"/>

</androidx.constraintlayout.widget.ConstraintLayout>

I have tried all properties i could find,alignText, gravity, alignLayout etc我已经尝试了我能找到的所有属性,alignText、gravity、alignLayout 等

Just change Item layout root element width property from wrap_content to match_parent , so each row item takes full RecyclerView's width and child text would be able to be centered on that width:只需将项目布局根元素的宽度属性从wrap_content更改为match_parent ,因此每个行项目都采用 RecyclerView 的完整宽度,并且子文本将能够以该宽度为中心:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent" // this is the change you have to make
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/listItem"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@font/caviar_dreams"
        android:textSize="24sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:paddingBottom="15dp"/>

</androidx.constraintlayout.widget.ConstraintLayout>

In the item layout just use:在项目布局中,只需使用:

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

    <TextView
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        .../>

</LinearLayout>

在此处输入图像描述

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

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