如何在材质组件的材质按钮中设置渐变背景? [英] How to set a gradient background in a Material Button from Material Components?
问题描述
我一直在尝试为 Android 的 Material Components 中的 Material Button 设置渐变背景,但它不起作用.那么,如何在 Material Button 中设置渐变背景?
I've been trying to set a gradient background in a Material Button from Material Components for Android, but it's not working. So, how to set a gradient background in a Material Button?
推荐答案
来自 MaterialButton
的文档:
From the documentation for MaterialButton
:
不要使用 android:background
属性.MaterialButton 管理自己的背景 drawable,设置一个新的背景意味着 MaterialButton
不能再保证它引入的新属性会正常工作.如果更改默认背景,MaterialButton
不能保证良好定义的行为.
Do not use the
android:background
attribute. MaterialButton manages its own background drawable, and setting a new background meansMaterialButton
can no longer guarantee that the new attributes it introduces will function properly. If the default background is changed,MaterialButton
cannot guarantee well-defined behavior.
唯一支持的修改按钮背景的方法是为 app:backgroundTint
属性设置颜色值.不幸的是,这里也不支持渐变;您只能使用简单的颜色值或 ColorStateList
.
The only supported way to modify the background of the button is to set a color value to the app:backgroundTint
attribute. Unfortunately, gradients are not supported here either; you can only use a simple color value or a ColorStateList
.
因此,不支持通过 MaterialButton 使用渐变背景的方法.
So, there is no supported way to use a gradient background with MaterialButton.
这篇关于如何在材质组件的材质按钮中设置渐变背景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!