如何在自定义按钮内向图像添加边距

问题描述

我有一个带有居中(认)文本的自定义按钮。我想在此文字的左侧添加8dp的图片,该图片必须位于自定义按钮中。

我只能将其添加ComponentFactoryResolver属性。我想使其更加居中。

enter image description here

这是我的自定义按钮。

如何使该图像居中? (文本左侧8dp)

解决方法

只需将MaterialButtonapp:iconGravity属性一起使用。

<com.google.android.material.button.MaterialButton
    style="@style/Widget.MaterialComponents.Button.Icon"
    app:icon="@drawable/...."
    app:iconGravity="textStart"
    android:text="@string/..."
    .../>

enter image description here

您可以使用app:iconPadding="xxdp"属性更改图标和文本之间的填充。

app:iconPadding="0dp"的示例:

enter image description here