[英]Android ActionBar not changing background color
我正在尝试更改操作栏的背景颜色,但它没有改变。 我的应用程序正在使用AppCompat主题,如果它影响任何东西。
这是我正在使用的主题。 我的应用程序指向使用此主题,这不是问题
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
<item name="actionBarStyle">@style/ActionBar</item>
</style>
<!-- Action Bar Styles -->
<style name="ActionBar" parent="Widget.AppCompat.ActionBar">
<item name="android:background">@color/blue</item>
<item name="background">@color/blue</item>
</style>
在ActionBar样式中,删除android:background,仅保留background属性。 还要确保在应用程序标记或扩展AndroidManifest.xml文件中操作栏的活动中实现AppTheme。 我还在Widget.AppCompat之前添加了“@ style /”属性,因为它是在android库中定义的默认样式。
您的代码应如下所示:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
<item name="actionBarStyle">@style/ActionBar</item>
</style>
<!-- Action Bar Styles -->
<style name="ActionBar" parent="@style/Widget.AppCompat.ActionBar">
<item name="background">#0000ff</item>
</style>
您也可以从代码中获取此信息
ab.setBackgroundDrawable(new ColorDrawable(newcolor));
以防您的xml无法正常工作
根据这个链接, https://developer.android.com/reference/android/R.attr.html#background它似乎没有采用颜色资源,所以你要么给它一个十六进制颜色值或创建一个drawable,使用你想要的颜色。 您可以尝试以下替代方案,看看它是否有效。
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
<item name="actionBarStyle">@style/ActionBar</item>
</style>
<!-- Action Bar Styles -->
<style name="ActionBar" parent="Widget.AppCompat.ActionBar">
<item name="android:background">#0000ff</item>
<item name="background">#0000ff</item>
</style>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.