android - DrawableLeft 与文本居中

标签 android android-layout

我无法将我的 drawableLeft 图标居中。
我可以轻松地将图标放在文本的左侧,但如果我将引力设置为居中,则只有文本居中,而没有图标。

<Button
    android:id="@+id/patient_list_button"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/patientList"
    android:drawableLeft="@drawable/ic_icon_two_heads"
    android:layout_marginBottom="15dp"
    style="@style/darkBlueButtonWithImage"/>

这就是我想要的:
enter image description here
这是我的:
enter image description here

<style name="darkBlueButtonWithImage">
    <item name="android:drawablePadding">10dp</item>
    <item name="android:textColor">@color/white</item>
    <item name="android:paddingLeft">10dp</item>
    <item name="android:background">@drawable/radius_dark_blue_button</item>
    <item name="android:gravity">center_vertical|left</item>
    <item name="android:drawableTint">@color/white</item> <!-- has to be set in activity.java -->
</style>

我怎样才能做到这一点?

最佳答案

终于,经过这么多年,我们有可能以简单直观的方式实现这种行为。 您所要做的就是使用 MaterialButton来自谷歌 Material 库。然后使用带有图标的样式。之后,您可以使用 app:iconGravity 属性并将其设置为 textStart

    <com.google.android.material.button.MaterialButton
        style="@style/Widget.MaterialComponents.Button.Icon"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Some text"
        app:iconGravity="textStart"
        app:icon="@drawable/some_icon" />

关于android - DrawableLeft 与文本居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43098974/

相关文章:

android - 使用多模块 android 库的 Gradle 配置

android - Android模拟器如何支持其他语言?

android - 警告对话框不关闭

java - 如何从 URL 加载不带扩展名的图像

Android ValueAnimator 正弦波

android - 在 API 16 及更低版本中完全删除复选框的正确方法

android - 如何将 LinearLayout 放置在另一个 RelativeLayout 内的 LinearLayout 下方?

Android ListActivity - 如何在 ListView 下面添加一个 View ?

android - 在 Android 的 layout.xml 中使用派生类

android - 如何从膨胀的布局中访问 View ?