Cardview 中的居中文本 - Android Studio

多维塔

我目前正在回收器中使用卡片视图,尽管我付出了努力,但文本仍是左对齐的。

这是图书项目布局。

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

这是当前的输出。在此处输入图片说明

如您所见,文本未居中。

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

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

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

贪心

添加android:gravity="center"LinearLayout,你很高兴去

我看到你LinearLayout只有一个孩子并且没有特殊属性,所以你甚至可以直接删除整个LinearLayout地方,其中. 为了使孩子居中,您也可以使用父母和/或孩子TextViewCardViewextends FrameLayoutFrameLayoutandroid: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
        

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Cardview在Android Studio中3.5.1不显示

如何在cardview android studio中制作运行文本?

在CardView中以编程方式在Android Studio中创建RelativeView

cardView android studio 上的厚左边框

无法在当前主题CardView中找到样式'cardview style'在Android Studio 3.1.3中不起作用

Android中TextView的水平和垂直居中文本

如何在android studio中单击按钮时从LinearLayout添加和删除CardView?

如何在android studio中创建半重叠图像到cardview

Android Studio 为什么 CardView 不起作用?

Xamarin Android CardView 在 Visual Studio 2017 中使用 NuGet

在android中通过cardview创建视图

CardView 圆角未显示在 Android 预览中

如何在Android中动态创建CardView

Android中的Cardview自定义设计

如何在Android中获取CardView大小?

如何在Android中创建循环CardView?

CardView在Android L中未显示阴影

Android:如何在CardView中插入RecyclerView?

Android 4.4从CardView中删除阴影

如何在cardView android中添加listView?

如何在Cardview Android中添加动画?

在Android中设置cardview背景逻辑

CardView Text覆盖在android中的imageview上

Android 无法使用自定义样式在微调器中居中文本

二进制XML文件第2行:在android Studio中的android <21中,膨胀类android.support.v7.widget.CardView时出错

谁能告诉我为什么此文本不在Android Studio中居中?

Android CardView 在 2 Android 设备中的行为不同

找不到android.support.v7.widget.CardView(Android Studio 1.1预览版)

strokeColor和strokeWidth在androidx.cardview.widget.CardView android中不起作用