如何在“材质组件"的“材质按钮"中设置渐变背景? [英] How to set a gradient background in a Material Button from Material Components?
问题描述
我一直在尝试从Android的Material Components中的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?
推荐答案
From the documentation for MaterialButton
:
请勿使用
android:background
属性. MaterialButton管理其自己的背景可绘制对象,并且设置新的背景意味着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屋!