【问题标题】:I am struggling to control the gravity of a textView inside my CardView items once I put it inside a GridView一旦我将它放在 GridView 中,我正在努力控制我的 CardView 项目中的 textView 的重力
【发布时间】:2020-01-13 04:26:22
【问题描述】:

下面是我当前gridView的截图: 即使我已将卡片内的 TextField 居中,它们仍然固定在左侧。如何解决此问题以使文本居中居中?似乎一旦我将一个项目添加到 gridView,gridView 属性就会推翻他们的孩子。

这是我的布局代码:

卡片浏览

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

【问题讨论】:

  • 你在TextView中尝试过android:gravity="center"
  • 在LinearLayout 中添加您的ImageView 和TextView,并将LinearLayout 放在CardView 中

标签: android kotlin gridview cardview


【解决方案1】:

在您的文本视图中添加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"/>

【讨论】:

    【解决方案2】:

    使用 ConstraintLayout 的相对布局坚持。

    对文本视图使用 center_horizo​​ntal=true

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-06-25
        • 2020-12-04
        • 2012-01-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-20
        相关资源
        最近更新 更多