android - 在按钮上,文本和可绘制对象在居中时距离太远

标签 android android-layout android-button material-components material-components-android

当我使按钮宽度与父级匹配时,可绘制的开始和文本距离太远,而且它们似乎不符合 android:drawablePadding 设置。我也尝试过 android:gravity="center"android:textAlignment="center" 但无济于事。设置一些 paddingStart/End 会间接影响它们之间的距离。 查看下面的结果:

match parent width button

按钮代码:

<Button
            style="@style/ActionButton.Primary.Light.Large"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_margin="0dp"
            android:drawableLeft="@drawable/ic_filter_and_sort_white"
            android:drawablePadding="0dp"
            android:textAlignment="center"
            android:text="Apply filters"
            app:layout_constraintBottom_toTopOf="@id/someViewBelow"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/someViewAbove" />

样式定义了background, textColor, margins, minDimensions, paddings , radiuses, textSize, textAllCaps, radiuses, stateListAnimator, fontPath - 所以没有什么会影响我正在寻找的东西。

最佳答案

使用 MaterialButton使用 app:iconGravity="start" 并使用 app:iconPadding 属性定义图标和文本之间的填充。

类似于:

    <com.google.android.material.button.MaterialButton
        style="@style/Widget.MaterialComponents.Button.Icon"
        app:icon="@drawable/...."
        app:iconGravity="start"
        app:iconPadding="..."

这个值可以是负数。

enter image description here

否则,您可以使用 app:iconGravity="textStart"
这里使用starttextStart作为iconGravity的区别。

enter image description here

关于android - 在按钮上,文本和可绘制对象在居中时距离太远,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58410419/

相关文章:

java - AsyncTaks 的受控执行

android - TextView在顶部和底部重叠

android - 使用来自 2 个不同布局的相同 Android 绑定(bind)类

android - 图像+文本在按钮中居中对齐

android - MaterialButton 和简单的 Button 有什么区别吗?

java - Android - dlopen 失败 : file offset for the library

android - jQuery UI 可排序和可拖动不适用于 iPad、iPhone 或 Android

Android:如何获取默认应用样式?

android - 生成 APK - 无法识别 gradlew

android - 在横向模式下支持不同的屏幕尺寸