如何在Android材质按钮中的文本旁边显示一个图标? [英] How to get an icon to show up next to text in Android Material Buttons?

查看:462
本文介绍了如何在Android材质按钮中的文本旁边显示一个图标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用由Google开发的材质按钮".由于第三方的依赖性,我无法使用androidx存储库,因此我目前正在使用android支持库(v28.0.0-alpha1,alpha3不允许我使用材质按钮)

I'm trying to use the Material Button as developed by google. Due to a third party dependency I cannot use the androidx repository, so I'm currently using the android support library (v28.0.0-alpha1, alpha3 did not allow me to use the material button)

我正在尝试实现一个宽按钮,该按钮带有居中的文本+文本旁边的图标:

I'm trying to achieve this a wide button with centered text + icon next to text:

但是我所能得到的就是按钮边缘上带有图标的居中文本:

But all I can get is this, centered text with icon on the edge of the button:

这与原始的android按钮相同,但也存在相同的问题.想法是材质按钮"可以解决此问题,但在28.0.0-alpha1上对我似乎不起作用

This is the same as the original android button, which suffered from the same problem. The idea was that the Material Button would solve this issue, but it doesn't seem to work for me on 28.0.0-alpha1

有一些解决方案,其中包括使用compounddrawables创建文本视图并在其周围绘制框架,但我想尽可能地坚持使用Material按钮.

There are solutions involving making a textview with compounddrawables and drawing a frame around it, but I'd like to stick to the material button if possible.

有人知道如何解决这个问题吗?

Anyone got a clue on how to fix this?

推荐答案

我也遇到了这个问题. 进行了一些挖掘,发现了这一点.

I'm having this issue too. Did a bit of digging around, and found this.

有一个名为app:iconGravity的属性,它具有两个选项,分别为 start textStart ,这似乎对我不起作用,但可以尝试一下-这是从材质按钮的GitHub存储库.

There's an attribute called app:iconGravity which has two options namely start and textStart which doesn't seem to work for me, but maybe try it - that is from the GitHub repo for the Material Button.

<com.google.android.material.button.MaterialButton
    android:id="@+id/material_icon_button"
    style="@style/Widget.MaterialComponents.Button.Icon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/icon_button_label_enabled"
    app:icon="@drawable/icon_24px"
    app:iconGravity="textStart"/>

https://github.com/material-components/material-components-android/blob/master/lib/java/com/google/android/material/button/MaterialButton.java (搜索iconGravity的定义,它在课程的开头)

https://github.com/material-components/material-components-android/blob/master/lib/java/com/google/android/material/button/MaterialButton.java (search for definition of iconGravity, it's in the beginning of the class)

这篇关于如何在Android材质按钮中的文本旁边显示一个图标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆