使用 AppCompat 更改操作栏的背景颜色 [英] Change Background color of the action bar using AppCompat

查看:38
本文介绍了使用 AppCompat 更改操作栏的背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网上发现了一些有关此问题的问题.不幸的是,到目前为止我尝试的一切都没有成功.

I found some questions about this problem around the web. Unfortunately, everything i try so far, has been unsuccessful.

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

Has the title say, i need to change the background color of my action bar.

该项目的最小 sdk 为 9,最大 sdk 为 19.

The project have a min sdk of 9, and max sdk of 19.

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

I have create in my res/values folder, an xml file:

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

the colors.xml stored in res/values

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

以及我更改主题的清单部分

and the part of the manifest where i change the theme

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

但没有任何改变.问题出在哪儿?应用程序接受代码,因为如果我更改这些代码:

But nothing changes. Where is the problem? The application accepts the code because if i change ths:

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

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

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

it does change the theme of my app, so the problem is in the style but I don't know how to solve it.

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

推荐答案

<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 版本.

you need both android:background and background items. The former is for newer versions of android that support ActionBar natively. The latter is for older android versions.

编辑

代替 使用

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

来自 SDK 21

要改变Action Bar的背景色,把这个加到ActionBarTheme中,两个颜色要不一样否则不行(感谢@Dre@lagoman)

to change Action Bar background color, add this to ActionBarTheme, and the two colours are to be different or it will not work (thanks to @Dre and @lagoman)

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

这篇关于使用 AppCompat 更改操作栏的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆