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

Bom*_*olo 45 xml android android-actionbar

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

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

该项目的最小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>
Run Code Online (Sandbox Code Playgroud)

colors.xml存储在res/values中

<resources>
    <color name="red">#FF0000</color>
</resources>
Run Code Online (Sandbox Code Playgroud)

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/CustomActionBarTheme" >
Run Code Online (Sandbox Code Playgroud)

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

<style name="CustomActionBarTheme" parent="@style/Theme.AppCompat.Light">
Run Code Online (Sandbox Code Playgroud)

<style name="CustomActionBarTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar ">
Run Code Online (Sandbox Code Playgroud)

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

<?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>
Run Code Online (Sandbox Code Playgroud)

Bla*_*elt 119

<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>
Run Code Online (Sandbox Code Playgroud)

你需要android:backgroundbackground物品.前者适用于本机支持ActionBar的较新版本的android.后者适用于较旧的Android版本.

编辑

而不是<resources>使用

<resources xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android">
Run Code Online (Sandbox Code Playgroud)

来自SDK 21

要更改操作栏背景颜色,将其添加到ActionBarTheme,两种颜色不同或不起作用(感谢@Dre@lagoman)

<item name="colorPrimary">@color/my_awesome_red</item> 
<item name="colorPrimaryDark">@color/my_awesome_darker_red</item>
Run Code Online (Sandbox Code Playgroud)

  • 似乎没什么用.令人惊讶的是,你应该花多少时间做一件简单的事情来设置动作栏的背景色...... (11认同)
  • 如果您已将SDK更新为API级别21,要更改操作栏背景颜色,请将其添加到`ActionBarTheme`:<item name ="colorPrimary"> @ color/my_awesome_red </ item> <item name ="colorPrimaryDark"> @ color/my_awesome_darker_red </ item>这是根据:[http://android-developers.blogspot.com/2014/10/appcompat-v21-material-design-for-pre.html](http://android-developers) .blogspot.com/2014/10 /程序兼容性-V21的材料设计换pre.html) (5认同)
  • 需要注意的是,colorPrimary和colorPrimaryDark不应该是相同的颜色.如果是,Android将不会为操作栏着色. (3认同)
  • 我无法告诉你,在我找到这个之前,我倾注了多少线程.万分感谢! (3认同)

Zoh*_*han 19

试试这个:

<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>
Run Code Online (Sandbox Code Playgroud)

  • 工作比投票的答案.谢谢! (2认同)

小智 9

试试这个.

<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
    <item name="colorPrimary"> #6699FF </item>
</style>
Run Code Online (Sandbox Code Playgroud)

您可以#6699FF根据您的选择更改颜色(或使用颜色资源).