如何根据其长度对齐 TextView

How to align a TextView according to its length

我有一个界面,其中有两个 ImageButton 和一个 TextView。 TextView 包含一个数字,两个 ImageButton 控制该数字,添加或删除 1。 问题是当数字变成两位数时:TextView 宽度增加并且两个 ImageButtons 之一移动。

如您所见,当数字是一位数时没有任何类型的问题,但当它变成两位数时,ImageButton 会从他的位置移动。我已经尝试使用 android:layout_gravityandroid:gravity 但它们不起作用。有办法保持 TextView "centered" 并确保 ImageButton 不移动吗?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:autofit="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingStart="5dp">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center_vertical"
        android:orientation="vertical">

        <TextView
            android:id="@+id/foodName"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginEnd="10dp"
            android:gravity="center_vertical"
            android:hint="food_name"
            android:maxLines="2"
            android:textAllCaps="false"
            android:textColor="@android:color/black"
            android:textSize="18sp"
            android:textStyle="bold" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end"
        android:gravity="end"
        android:orientation="horizontal"
        android:padding="5dp">

        <ImageButton
            android:id="@+id/addFood"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@null"
            app:srcCompat="@drawable/ic_add_black_40dp" />

        <TextView
            android:id="@+id/foodQuantity"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center|center_horizontal"
            android:layout_marginStart="10dp"
            android:layout_marginEnd="10dp"
            android:gravity="center|center_horizontal"
            android:maxLength="2"
            android:text="0"
            android:textColor="#707070"
            android:textSize="20sp"
            android:textStyle="bold" />

        <ImageButton
            android:id="@+id/removeFood"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@null"
            app:srcCompat="@drawable/ic_remove_black_40dp" />

    </LinearLayout>

</LinearLayout>

这是我的代码,这是 RecyclerView 成员的布局,因为我创建了一个个性化元素。 非常感谢。

我建议您使用 android:layout_weight,这样您就可以在必要时定义哪个元素填充剩余的 space。因此,当将 TextView a layout_weight 设置为 1 并将 ImageButtons a layout_weight 设置为 0 时,您可以随后将定义 TextView 内部重力的 TextView 的 android:gravity 设置为 center 并且它将始终居中。

BUT:您需要将 LinearLayout 的宽度设置为 match_parent 以便它填充父级。否则 LinearLayout 永远不会得到一些剩余的 space 因为它总是缩小到所有元素一起需要的大小

由于您的 foodQuantity TextView 上有 wrap_content,因此 addFood 将移动以适应添加多个数字所需的额外间距。如果你想保持按钮的位置固定,并确保按钮不移动,那么你将不得不给你的 TextView 一个固定的宽度以容纳你可以预期的最大字符数.

...
...
<LinearLayout
   android:id="@+id/layout_count"
   android:layout_width="200dp"
   or android:layout_width="match_parent
   android:layout_height="wrap_content"
   android:orientation="vertical">

   <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:orientation="horizontal"
       android:gravity="center_vertical">

       <ImageView
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:src="@drawable/ic_complete" />

       <TextView
           android:layout_width="match_parent"
           android:layout_weight="1"
           android:gravity="center"
           android:layout_height="wrap_content"
           android:text="1" />

      <ImageView
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:src="@drawable/ic_complete" />

   </LinearLayout>

   <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:orientation="horizontal"
       android:gravity="center_vertical">

       <ImageView
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:src="@drawable/ic_complete" />

       <TextView
           android:layout_width="match_parent"
           android:layout_weight="1"
           android:gravity="center"
           android:layout_height="wrap_content"
           android:text="1" />

      <ImageView
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:src="@drawable/ic_complete" />

   </LinearLayout>

</LinearLayout>