gpt4 book ai didi

android - DrawableLeft 与文本居中

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:35:28 24 4
gpt4 key购买 nike

我无法将我的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com