具有MaterialComponents主题的ActionBar背景 [英] ActionBar background with MaterialComponents theme
问题描述
我想自定义我的ActionBar.我的主题如下:
I want to customize my ActionBar. My Theme looks as below:
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
<item name="actionBarStyle">@style/MyActionBar</item>
</style>
<style name="MyActionBar" parent="@style/ThemeOverlay.MaterialComponents.ActionBar">
<item name="background">@drawable/actionBarBackground</item>
</style>
在值"文件夹中:
<drawable name="actionBarBackground">#FFFFFFFF</drawable>
在values-night文件夹中:
In values-night folder:
<drawable name="actionBarBackground">#FF000000</drawable>
不确定为什么我的ActionBar背景颜色没有相应更改.我也尝试过以其他方式更改主题,但是没有任何效果.这是我的其他尝试:
Not sure why my ActionBar background colour doesn't change accordingly. I have tried to change my theme in different other ways as well but nothing works. Here are my other tries:
我使用了 actionBarTheme
而不是 actionBarStyle
.
<item name="actionBarTheme">@style/MyActionBar</item>
我也尝试使用 colorPrimary
.
<item name="colorPrimary">@color/actionBarBackground</item>
我想念什么吗?
推荐答案
由于您使用的是 Theme.MaterialComponents.DayNight
应用主题,因此 ActionBar
背景色由默认为 colorPrimary
属性.
Since you are using a Theme.MaterialComponents.DayNight
app theme the ActionBar
background color is defined by default by the colorPrimary
attribute.
<style name="AppThemeActionBar" parent="Theme.MaterialComponents.DayNight">
<item name="colorPrimary">@color/mycolor</item>
</style>
其中 mycolor
是在 res/values/colors.xml
<resources>
<color name="mycolor">#xxxxxx</color>
...
</resources>
您还可以使用应用主题中的 actionBarStyle
属性来自定义背景颜色.
像这样:
You can also customize the background color using the actionBarStyle
attribute in your app theme.
Something like:
<style name="AppThemeActionBar" parent="Theme.MaterialComponents.DayNight">
<item name="actionBarStyle">@style/Custom.ActionBar</item>
...
</style>
其中:
<style name="Custom.ActionBar" parent="Widget.MaterialComponents.Light.ActionBar.Solid">
<item name="background">@color/mycolor</item>
</style>
否则,您可以使用 actionBarTheme
属性覆盖背景色:
Otherwise you can use the actionBarTheme
attribute to override the background color:
<style name="AppThemeActionBar" parent="Theme.MaterialComponents.DayNight">
<item name="actionBarTheme">@style/ThemeOverlay.ActionBar</item>
...
</style>
<style name="ThemeOverlay.ActionBar" parent="">
<item name="colorPrimary">@color/mycolor</item>
</style>
这篇关于具有MaterialComponents主题的ActionBar背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!