我在Android Studio中有以下XML段代码:

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="48dp"
    android:layout_height="48dp"
    android:layout_marginLeft="8dp">

    <ImageButton
        android:id="@+id/btnEditPlants"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:contentDescription="@string/btnEditPlant_Text"
        android:scaleType="fitCenter"
        android:src="@drawable/ca_edit_plant"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/tvNrOfCurrentlySelectedItemsCounter"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/layout_plants_selection_counter_rect"
        android:textColor="@color/md_theme_dark_primary"
        android:textStyle="bold"
        android:textSize="12sp"
        android:alpha="0.6"
        android:textAlignment="center"
        android:visibility="visible"
        android:text="99"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

The purpose of this ConstraintLayout is to introduce selected items counter (showing int number) overlayed on a top of ImageButton. The counting works fine, however I cannot understand why text is not centered, as you can seen from design and runtime screenshots: TextView's text in design time TextView's text at runtime
Why TextView, named tvNrOfCurrentlySelectedItemsCounter is not centering the text. It has android:textAlignment="center" property set, as you can see. This TextView background is declared as shape:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/md_theme_light_onSecondaryContainer" />
    <corners android:radius="8dp"/>
    <corners android:radius="8dp"/>
    <size android:height="24dp"
        android:width="24dp"/>
</shape>

推荐答案

您的文本没有居中的原因是它缺少android:gravity="center",但我也会将它们的宽度/高度更改为0dp(图像和文本),所以约束布局发挥了作用,它在这种情况下应该不会有什么不同,但(对我来说)更明确.

我复制了您的布局并进行了更改(抱歉,我没有您的资源/ colored颜色 等)

ImageView with a centered text that says 99

如果您想知道"为什么/使用哪一个",如果我没记错的话,还有一个更复杂的答案可以追溯到API 17.

简而言之,gravityTextView的属性,而textAlignment来自普通的View.

(最终,大多数/所有Android小部件都是从它继承而来的).

如您所见,文本对齐的选项要少得多,因为所有视图根本不需要(或至少不提供)文本或文本定制,而TextView需要它们.

这让人困惑吗?是.

是否应该有一个更明确的代表性?是.

欢迎来到安卓开发.

Android相关问答推荐

如何自定义所选的NavigationBar项目?

Kotlin多平台向导,不兼容版本(ANP 8.2.0)ANP 8.1.2

带有kSP而不是kapt的Hilt

Jetpack编写Lazy列滑动删除动画不顺利结束

LocalContext.current的问题(@Composable调用只能从@Composable函数的上下文发生)

登录方法显示为空列表,即使它显示的是Firebase身份验证控制台

在内部创建匿名对象的繁忙循环调用函数会产生开销吗?

可以';t将数据插入房间数据库

Dispatchers中的Kotlin协同程序.Main没有';t块主螺纹

Android Studio SQLite 错误:列不正确(没有这样的列:_id)

Kotlin - 在继续之前如何等待这个协程完成?

Jetpack Compose 使用 SavedStateHandle 发送返回结果不适用于 ViewModel 中注入的 SavedStateHandle

发布 MAUI 应用程序时出现遇到重复程序集错误

如何将一个 Composable 作为其参数传递给另一个 Composable 并在 Jetpack Compose 中显示/运行它

我该怎么做文本计时器

从活动共享视图模型以使用 hilt 组合函数

如何在jetpack compose中通过lamda返回columnScope/RowScope

可组合的可见性不随状态变化而变化

Kotlin:如何在另一个变量的名称中插入一个变量的值

Kotlin Compose 全局页脚视图