在 themes.xml 中,style的parent如下所示。

1
<style name="Theme.AndroidUiDesign" parent="Theme.MaterialComponents.DayNight.NoActionBar">

这时候,app中按钮的背景颜色会是主题色,自己设置的背景色则会被主题色覆盖。

修改成下面这样子就可以正常显示自定义背景颜色了。

1
<style name="Theme.AndroidUiDesign" parent="Theme.MaterialComponents.DayNight.NoActionBar.Bridge">

material design官方文档是这样解释的。

Bridge Themes

If you cannot change your theme to inherit from a Material Components theme, you can inherit from a Material Components Bridge theme.

1
2
3
<style name="Theme.MyApp" parent="Theme.MaterialComponents.Light.Bridge">
<!-- ... -->
</style>

Both Theme.MaterialComponents and Theme.MaterialComponents.Light have .Bridge themes:

  • Theme.MaterialComponents.Bridge
  • Theme.MaterialComponents.Light.Bridge
  • Theme.MaterialComponents.NoActionBar.Bridge
  • Theme.MaterialComponents.Light.NoActionBar.Bridge
  • Theme.MaterialComponents.Light.DarkActionBar.Bridge

Bridge themes inherit from AppCompat themes, but also define the new Material Components theme attributes for you. If you use a bridge theme, you can start using Material Design components without changing your app theme.

大致意思就是,如果我们既想要保留我们的自定义样式,比如按钮背景啥的,又想要用这种比较新的主题,就可以使用Bridge主题。毕竟是桥嘛,两边都要也很合理。