一旦将它放入 GridView 中,我就很难控制 CardView 项目中 textView 的重力

I am struggling to control the gravity of a textView inside my CardView items once I put it inside a GridView

下面是我的gridView目前的截图: 即使我已将 TextField 置于卡内的中心位置,它们仍固定在左侧。我该如何解决这个问题,使文本居中?似乎一旦我将一个项目添加到 gridView,gridView 属性就会否决它们的子项。

这是我的布局代码:

CARDVIEW

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView 
xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    layout_height=""
    android:layout_width="120dp"
    android:layout_height="120dp"
    app:cardBackgroundColor="#E73737"
    app:cardElevation="4dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

<androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/relativeLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <ImageView
            android:id="@+id/ivCategoryIcon"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_marginTop="16dp"
            android:layout_marginBottom="8dp"
            app:layout_constraintBottom_toTopOf="@+id/tvCategoryTitle"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.498"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@drawable/btn_translation_panel_loading" />

    <TextView
            android:id="@+id/tvCategoryTitle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="2dp"
            android:layout_marginBottom="8dp"
            android:text="TextView"
            android:textAlignment="center"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/ivCategoryIcon" 
/>

</androidx.constraintlayout.widget.ConstraintLayout>


</androidx.cardview.widget.CardView>

片段

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/frameLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.fragments.CategoryViewFragment">

<GridView
        android:id="@+id/gvCategories"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="32dp"
        android:layout_marginEnd="32dp"
        android:clickable="true"
        android:clipChildren="false"
        android:gravity="center"
        android:horizontalSpacing="8dp"
        android:numColumns="2"
        android:stretchMode="columnWidth"
        android:verticalSpacing="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:visibility="visible" />
</androidx.constraintlayout.widget.ConstraintLayout>

在您的文本视图中添加 android:gravity="center",如下所示。

<TextView
     android:id="@+id/tvCategoryTitle"
     android:layout_width="0dp"
     android:layout_height="wrap_content"
     android:layout_marginTop="2dp"
     android:layout_marginBottom="8dp"
     android:text="TextView"

     android:gravity="center" 

     android:textAlignment="center"
     app:layout_constraintBottom_toBottomOf="parent"
     app:layout_constraintEnd_toEndOf="parent"
     app:layout_constraintStart_toStartOf="parent"
     app:layout_constraintTop_toBottomOf="@+id/ivCategoryIcon"/>

坚持使用ConstraintLayout的相对布局。

对文本使用center_horizontal view=true

你能试试这个并改变你的约束布局吗?

<androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/relativeLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        <ImageView
                android:id="@+id/ivCategoryIcon"
                android:layout_width="80dp"
                android:layout_height="80dp"
                android:layout_marginTop="16dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:srcCompat="@drawable/btn_translation_panel_loading" />

        <TextView
                android:id="@+id/tvCategoryTitle"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="2dp"
                android:layout_marginBottom="8dp"
                android:text="TextView"
                android:textAlignment="center"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/ivCategoryIcon"
                />

    </androidx.constraintlayout.widget.ConstraintLayout>