【问题标题】:How do i center the items of a Recycler View?如何将 Recycler View 的项目居中?
【发布时间】:2020-10-28 13:08:57
【问题描述】:

我已经设法制作了一个 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" />

项目:

<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>

我已经尝试了我能找到的所有属性,alignText、gravity、alignLayout 等

【问题讨论】:

标签: android text android-recyclerview alignment


【解决方案1】:

只需将项目布局根元素的宽度属性从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>

【讨论】:

  • 由于设计更改,它似乎已居中。变化是添加了这些值,使其四舍五入并为其提供背景。现在的问题是文本不能以其 bg 居中正确。 android:background="@drawable/rounded" android:ems="10" android:alpha="0.7"
【解决方案2】:

在项目布局中只需使用:

<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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-31
    • 1970-01-01
    • 1970-01-01
    • 2015-07-11
    • 2021-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多