如何减少按钮内按钮文本和图标之间的间距?

How to reduce the spacing between Button text and icon within a button?

我在button里面放了一个image(图标)和text,如图。

我想把button里的image移到text旁边,但是我不能用padding,我也不能用drawableLeft属性。

有什么好的方法吗?

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginLeft="12dp"
    android:layout_marginRight="12dp"
    app:cardElevation="10dp">
    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <LinearLayout
            android:id="@+id/ll1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            app:layout_constraintTop_toTopOf="parent">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="8dp"
                android:text="TEST"/>
            <View
                android:layout_width="match_parent"
                android:layout_height="2dp"
                android:background="@color/light_blue_400"/>
        </LinearLayout>

        <LinearLayout
            android:id="@+id/ll2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            app:layout_constraintTop_toBottomOf="@+id/ll1">
            <Button
                android:id="@+id/delete"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_marginStart="8dp"
                android:layout_marginEnd="4dp"
                app:icon="@drawable/ic_delete"
                android:text="delete"/>

            <Button
                android:id="@+id/add"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_marginEnd="8dp"
                android:layout_marginStart="4dp"
                app:icon="@drawable/ic_add"
                android:text="add"/>
        </LinearLayout>

        <androidx.recyclerview.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toBottomOf="@+id/ll2"/>
    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>

这是一个通过操纵填充的解决方案:

  • 将负填充调整为app:iconPadding

  • 将正填充调整为android:paddingStart

     <Button
         android:id="@+id/delete"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:layout_marginStart="8dp"
         android:layout_marginEnd="4dp"
         android:layout_weight="1"
         android:backgroundTint="@color/black"
         android:paddingStart="60dp"
         android:paddingLeft="60dp"
         android:text="delete"
         android:textColor="@color/white"
         app:icon="@drawable/ic_baseline_remove_24"
         app:iconPadding="-40dp"
         app:iconTint="@color/white" />
    
     <Button
         android:id="@+id/add"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:layout_marginStart="4dp"
         android:layout_marginEnd="8dp"
         android:layout_weight="1"
         android:backgroundTint="@color/black"
         android:paddingStart="50dp"
         android:paddingLeft="50dp"
         android:text="add"
         android:textColor="@color/white"
         app:icon="@drawable/ic_baseline_add_24"
         app:iconPadding="-50dp"
         app:iconTint="@color/white" />
    

预览: