不能将android:background与新材质组件中的按钮一起使用 [英] Can't use android:background with button from the new material components
问题描述
我在android x上使用了新的材质组件com.google.android.material:material
,但无法为按钮设置自定义背景.
I'm using the new material components com.google.android.material:material
with android x but I can't set a custom background to the button.
我知道我可以使用app:backgroundTint
更改颜色
I know that I can use app:backgroundTint
to change the color
但是默认背景有一些我要消除的填充,以及使用android:background
设置自己的背景的旧方法,但是这种方法不再起作用.
but the default background has some padding that I want to get rid of, and the old way of using android:background
to set my own background but this is no longer working.
我查看了 docs ,但找不到对此更改的任何提及.
I looked at the docs but can't find any mention to this change.
推荐答案
The documentation for the MaterialButton
class says:
请勿使用
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.
但是, GitHub自述文件说:
注意:
MaterialButton
在视觉上与Button
和AppCompatButton
不同.主要区别之一是AppCompatButton
在左侧和右侧都有一个4dp
插图,而MaterialButton
没有.
Note:
MaterialButton
is visually different fromButton
andAppCompatButton
. One of the main differences is thatAppCompatButton
has a4dp
inset on the left and right sides, whereasMaterialButton
does not.
这仅提及左/右插图,但自述文件的属性"部分显示,所有四个插图均受支持:
This mentions only left/right inset, but the Attributes section of the readme shows that all four insets are supported:
因此您可以将以下属性添加到您的<MaterialButton>
标记中:
So you could add these attributes to your <MaterialButton>
tag:
android:insetTop="0dp"
android:insetBottom="0dp"
这篇关于不能将android:background与新材质组件中的按钮一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!