使用AppCompat更改操作栏的背景颜色

48

我在网上找到一些关于这个问题的问答。不幸的是,迄今为止,我尝试的一切都没有成功。

正如标题所说,我需要更改操作栏的背景颜色。

该项目的最小SDK为9,最大SDK为19。

我在我的res/values文件夹中创建了一个XML文件:

red_actionbar.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="CustomActionBarTheme" parent="@style/Theme.AppCompat.Light">
        <item name="actionBarStyle">@style/MyActionBar</item>
    </style>
    <style name="MyActionBar"
           parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
        <item name="background">@color/red</item>
    </style>
</resources>

存储在res/values中的colors.xml

<resources>
    <color name="red">#FF0000</color>
</resources>

我修改主题的清单部分

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/CustomActionBarTheme" >

但是没有任何变化。问题出在哪里?应用程序接受代码,因为如果我更改了这个:

<style name="CustomActionBarTheme" parent="@style/Theme.AppCompat.Light">
<style name="CustomActionBarTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar ">

它确实改变了我的应用程序的主题,所以问题在于样式,但我不知道如何解决。

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="CustomActionBarTheme" parent="@style/Theme.AppCompat.Light">
        <item name="android:actionBarStyle" tools:ignore="NewApi">@style/MyActionBar</item>
        <item name="actionBarStyle">@style/MyActionBar</item>
    </style>
    <style name="MyActionBar"
           parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
        <item name="android:background"  tools:ignore="NewApi">@color/red</item>
        <item name="background">@color/red</item>
    </style>
</resources>

你所用的设备使用的是哪个版本的安卓系统?你在哪里进行代码测试? - Blackbelt
安卓4.2.2三星S2 Plus - Bombolo
1
主题定制总是很棘手。文档混乱,记录不全。另外,你需要支持最低 Android SDK 9 吗?这将覆盖大多数运行在 15 或更高版本的设备......仅供参考。 - angryITguy
4个回答

123
<style name="MyActionBar" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
    <item name="android:background"  tools:ignore="NewApi">@color/red</item>
    <item name="background">@color/red</item>
</style>

<style name="CustomActionBarTheme" parent="@style/Theme.AppCompat.Light">
    <item name="android:actionBarStyle"   tools:ignore="NewApi">@style/MyActionBar</item>
    <item name="actionBarStyle">@style/MyActionBar</item>

</style>

你需要同时拥有 android:backgroundbackground 这两个属性。前者适用于原生支持 ActionBar 的较新版本的 Android 系统,而后者适用于较旧版本的 Android 系统。

编辑

请使用 <resources>。

<resources xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android">

从SDK 21开始,若要更改操作栏(Action Bar)的背景颜色,将以下内容添加到ActionBarTheme中。两种颜色必须不同,否则无效(感谢@Dre@lagoman)。

<item name="colorPrimary">@color/my_awesome_red</item> 
<item name="colorPrimaryDark">@color/my_awesome_darker_red</item>

1
如果我使用android:actionBarStyle,就会出现错误。android:actionBarStyle需要API级别11(当前最低级别为9)。 - Bombolo
5
如果你已经将SDK更新到API 21级别,想要更改操作栏背景颜色,请在ActionBarTheme中添加以下内容:@color/my_awesome_red @color/my_awesome_darker_red参考链接:http://android-developers.blogspot.com/2014/10/appcompat-v21-material-design-for-pre.html - Mladen
3
需要说明的是,colorPrimary和colorPrimaryDark不应该是相同的颜色。如果它们相同,Android将不会对操作栏进行着色。 - Dre
3
我找了很多篇无用的帖子,直到找到这一篇。非常感谢!我无法告诉你具体有多少篇。 - Cora
15
似乎一切都不起作用。让人惊讶的是,为了做一件简单的事情,比如设置操作栏的背景颜色,需要花费很多时间... - bashan
显示剩余6条评论

18

试一下这个:

<style name="CustomActionBarTheme" parent="@style/Theme.AppCompat.Light">
    <item name="android:actionBarStyle">@style/MyActionBar</item>
    <item name="actionBarStyle">@style/MyActionBar</item>

</style>

<style name="MyActionBar" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">

    <item name="android:background">@color/red</item>
    <item name="background">@color/red</item>

</style>

我无法使用。类型不匹配:无法将android.app.ActionBar转换为android.support.v7.app.ActionBar。我的活动扩展ActionBarActivity以支持SDK 9。 - Bombolo
我可以知道如何改变标题颜色吗? - N Jay
谢谢!我花了5个小时在这上面!! - TechSpellBound
我不明白为什么那个编辑被添加到被接受的答案中了? - TechSpellBound
只需将其复制并粘贴到styles.xml文件中吗?对我来说没什么效果 :( - Tony
此答案是以上答案之一的重复。 - Ivan

9

试一下这个。

<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
    <item name="colorPrimary"> #6699FF </item>
</style>

你可以根据自己的选择更改#6699FF颜色(或使用颜色资源)。

适用于我...很棒 - NitinM

1
尝试使用 <\p>。
parent="Theme.AppCompat.Light"

代替

parent="@style/Theme.AppCompat.Light"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接