我在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:
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>