Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/188.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 右侧带有图标的材质按钮_Android_Material Design_Android Button_Material Components_Material Components Android - Fatal编程技术网

Android 右侧带有图标的材质按钮

Android 右侧带有图标的材质按钮,android,material-design,android-button,material-components,material-components-android,Android,Material Design,Android Button,Material Components,Material Components Android,谷歌提供的服务真的很不错。 根据,我们可以设置默认显示在左侧的图标。有人知道怎么把它调右吗?我找不到这方面的任何信息。我今天在谷歌上搜索了同样的问题,发现了这个:android:layoutDirection=“rtl”。它在我的测试项目中起作用 解决方案1 对每个按钮使用此扩展方法(kotlin): fun MaterialButton.setRightIcon() { TextViewCompat.setCompoundDrawablesRelative(this, null, n

谷歌提供的服务真的很不错。

根据,我们可以设置默认显示在左侧的图标。有人知道怎么把它调右吗?我找不到这方面的任何信息。

我今天在谷歌上搜索了同样的问题,发现了这个:
android:layoutDirection=“rtl”
。它在我的测试项目中起作用

解决方案1 对每个按钮使用此扩展方法(kotlin):

fun MaterialButton.setRightIcon() { 
    TextViewCompat.setCompoundDrawablesRelative(this, null, null, this.icon, null) 
}
解决方案2(更好的方法) 扩展
MaterialButton
类以在任何地方使用:

class RtlMaterialButton(context: Context, attrs: AttributeSet) : MaterialButton(context, attrs) {

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        updateIcon()
    }
    private fun updateIcon() {
        TextViewCompat.setCompoundDrawablesRelative(this, null, null, icon, null)
    }

}
解决方案3(工作时间
minSDK>=17
) 如果您想对图标使用
textStart
gravity,可以对每个按钮使用
android:layoutDirection=“rtl”


在应用程序中设置
android:supportsRtl=“true”
AndroidManifest.xml
在您的
标签中设置
在发布版中为end添加了一个新的重力值

只需使用属性或方法即可

它需要android材质组件的版本1.1.0

您还可以使用app:iconGravity=“end”

还根据:

如果将开始对齐图标添加到此按钮,请使用类似于默认MaterialButton样式中指定的“.icon”样式之一的样式。“.Icon”样式稍微调整填充以实现更好的视觉平衡。此样式只能与“开始对齐”图标按钮一起使用。如果您的图标是末端对齐的,则不能使用“.icon”样式,而必须手动调整填充,以镜像视觉调整


似乎不可能。看看源代码,第669行,它总是添加到开始(左)Hum有趣,我认为这是可能的,因为文档说“默认”。很遗憾,他们不允许xml中的选择。但多亏了你,我找到了解决办法。我在活动中回忆起这个方法,并在右侧设置了图标:
TextViewCompat.setCompoundDrawablesRelative(button,null,null,button.icon,null)
@benjaminleNet我试图在按钮上设置compunddrawble以实现相同的功能,但似乎无法修复它。你能给我发代码或者告诉我你是如何在地图上画这个图标的吗right@Snake我刚刚做了一个扩展:
fun MaterialButton.setRightIcon(){TextViewCompat.setCompoundDrawablesRelative(this,null,null,this.icon,null)}
在视图初始化之后,我把它叫做
button.setRightIcon()
,哇,太棒了,谢谢!很久没见过这么复杂的黑客了。我如何在文本上方设置图标?