不能将android:background与新材质组件中的按钮一起使用 [英] Can't use android:background with button from the new material components

本文介绍了不能将android:background与新材质组件中的按钮一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在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 means MaterialButton 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在视觉上与ButtonAppCompatButton不同.主要区别之一是AppCompatButton在左侧和右侧都有一个4dp插图,而MaterialButton没有.

Note: MaterialButton is visually different from Button and AppCompatButton. One of the main differences is that AppCompatButton has a 4dp inset on the left and right sides, whereas MaterialButton 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屋!

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