简体   繁体   English

Android ActionBar不会改变背景颜色

[英]Android ActionBar not changing background color

I am trying to change the background color of my action bar but it doesnt change. 我正在尝试更改操作栏的背景颜色,但它没有改变。 My app is using the AppCompat theme if that affects anything. 我的应用程序正在使用AppCompat主题,如果它影响任何东西。

Here is my theme that is being used. 这是我正在使用的主题。 My app points to use this theme so that is not the problem 我的应用程序指向使用此主题,这不是问题

<!-- 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>

In your ActionBar style, delete the android:background, only keep the background attribute. 在ActionBar样式中,删除android:background,仅保留background属性。 Also make sure to implement your AppTheme in the application tag or in the activities extending the action bar in your AndroidManifest.xml file. 还要确保在应用程序标记或扩展AndroidManifest.xml文件中操作栏的活动中实现AppTheme。 I've also added the "@style/" attribute before Widget.AppCompat since its a default style defined in the android library. 我还在Widget.AppCompat之前添加了“@ style /”属性,因为它是在android库中定义的默认样式。

Your code should look like this : 您的代码应如下所示:

<!-- 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>

You can se this from code as well 您也可以从代码中获取此信息

ab.setBackgroundDrawable(new ColorDrawable(newcolor)); ab.setBackgroundDrawable(new ColorDrawable(newcolor));

Just in case your xml is not working 以防您的xml无法正常工作

According to this link, https://developer.android.com/reference/android/R.attr.html#background it doesn't seem to take color resource, so you either give it a hex color value or create a drawable that uses the color you want. 根据这个链接, https://developer.android.com/reference/android/R.attr.html#background它似乎没有采用颜色资源,所以你要么给它一个十六进制颜色值或创建一个drawable,使用你想要的颜色。 You could try the following alternative to see if it works. 您可以尝试以下替代方案,看看它是否有效。

<!-- 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM