本文介绍了在按钮上,居中时文本和可绘制对象距离太远的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使按钮宽度与父项匹配时,可绘制的开始位置和文本距离太远,它们似乎不尊重android:drawablePadding设置.我也尝试了android:gravity="center"android:textAlignment="center"无济于事.设置一些paddingStart/End会间接影响它们彼此之间的距离.看到下面的结果:

when I make a button width match parent, the drawable start and text are too far away, and they don't seem to respect android:drawablePadding setting. I tried as well with android:gravity="center" and android:textAlignment="center" to no avail. Setting some paddingStart/End affect indirectly how close they are to each other.See the result below:

按钮的代码:

<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" />

样式定义backgroundtextColormarginsminDimensionspaddingsradiusestextSizetextAllCapsradiusesstateListAnimatorfontPath-所以没有什么会影响我想要的东西.

the style defines background, textColor, margins, minDimensions, paddings, radiuses, textSize, textAllCaps, radiuses, stateListAnimator, fontPath - so nothing that should affect what I'm looking for.

推荐答案

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

Use the MaterialButton with app:iconGravity="start" and defining the padding between the icon and the text with the app:iconPadding attribute.

类似的东西:

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

此值可以为负.

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

Otherwise you can use app:iconGravity="textStart".
Here the difference of using start and textStart as iconGravity.

这篇关于在按钮上,居中时文本和可绘制对象距离太远的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 13:32