【问题标题】:Centre text inside a Cardview - Android StudioCardview 中的文本居中 - Android Studio
【发布时间】:2021-09-29 14:00:10
【问题描述】:

我目前在回收站内使用卡片视图,尽管我努力了,但文本是左对齐的。

这是图书项目布局。

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    app:cardCornerRadius="4dp"
    app:cardElevation="5dp"
    app:cardUseCompatPadding="true"
    app:cardBackgroundColor="@color/white">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >

        <TextView
            android:id="@+id/bookTitleTextView"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"

            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            android:layout_gravity="center_horizontal"
            />
    </LinearLayout>
</androidx.cardview.widget.CardView>

这是回收站本身。

<?xml version="1.0" encoding="utf-8"?>
<androidx.recyclerview.widget.RecyclerView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/bookRecycler" />

这是当前的输出。 enter image description here

如您所见,文本未居中。

我尝试使用布局重力使文本居中,但不起作用。 我还尝试在文本视图上使用约束布局并使用约束将文本居中而没有任何运气。

android:layout_gravity = "center" 已经尝试过了。

任何关于我所忽略的建议都会很棒。 谢谢。

【问题讨论】:

    标签: java android android-recyclerview center android-cardview


    【解决方案1】:

    LinearLayout 添加android:gravity="center",您就可以开始了

    我看到你的LinearLayout 只有一个孩子,没有特殊属性,所以你甚至可以删除整个LinearLayout - 将TextView 直接放在CardView 中,extends FrameLayout。要在 FrameLayout 中居中子级,您也可以使用 android:gravity="center" 作为父级和/或 android:layout_gravity="center" 作为子级

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.cardview.widget.CardView
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:gravity="center"
        app:cardCornerRadius="4dp"
        app:cardElevation="5dp"
        app:cardUseCompatPadding="true"
        app:cardBackgroundColor="@color/white">
    
        <TextView
            android:layout_gravity="center"
                ... rest of code
            
    

    【讨论】:

      【解决方案2】:

      android:gravity="center" 是正确的代码,而不是 android:layout_gravity="center_horizontal"

      【讨论】:

      • 是的,这也不起作用,我已经测试过了,忘记提及了。谢谢。
      • 如果 TextView android:layout_width="match_parent"android:gravity="center" 有效
      猜你喜欢
      • 2018-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-17
      • 1970-01-01
      相关资源
      最近更新 更多