更改MaterialButton的BackgroundTint

时间:2018-06-28 22:14:57

标签: android

我正在使用持久的支撑设计:28 ,alpha3。

我使用“ Theme.MaterialComponents.Light.NoActionBar” 作为应用程序的主题,并使用“ MaterialButton ”代替布局中的常规“ Button”

我可以正常设置XML中的BackgroundTind,但不能通过Java进行更改。

我尝试过:

!

但是它们都不起作用...我还尝试通过将setBackgroundTintList保留为null来清除当前色调,并且也不起作用。

1 个答案:

答案 0 :(得分:1)

我也无法正常工作。作为一种解决方法,我执行了以下操作:首先,获取当前的背景Drawable,然后使用所需的颜色对其进行着色,然后使用setBackgroundDrawable为材质按钮设置新的背景。

Drawable background = materialButton.getBackground();
background.setTint(ContextCompat.getColor(getContext(), R.color.bg_button_primary));
materialButton.setBackgroundDrawable(background);

我希望有帮助。