如何更改操作栏的颜色?

How to change the color of the actionbar?

我一直在尝试更改操作栏的颜色,但没有成功。我一直在寻找一段时间...... 我尝试了这个 post 的解决方案: How to change ActionBar color?

这是我的 styles.xml:

<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:actionBarStyle">@style/ActionBar</item>
</style>

<style name="ActionBar" parent="Widget.AppCompat.Light.ActionBar.Solid.Inverse">
    <item name="android:background">#0091ea</item>
</style>
</resources>

你可以这样

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

    <style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
        <item name="android:titleTextStyle">@style/TitleBarTextColor</item>
        <item name="android:background">YOUR_COLOR_CODE</item>
    </style>

    <style name="TitleBarTextColor" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
        <item name="android:textColor">YOUR_COLOR_CODE</item>
    </style>

显然,您必须在您的 AndroidManifest 中的 Activity 添加此主题。

编辑

将这些导入添加到您的 Activity

 import android.app.ActionBar;
 import android.graphics.drawable.ColorDrawable;

就在你 onCreate 的下面放这个

ActionBar ab = getActionBar(); 
       ColorDrawable colorDrawable = new ColorDrawable(Color.parseColor("#81a3d0"));     
       ab.setBackgroundDrawable(colorDrawable);

This works for me :

getActionBar().setBackgroundDrawable(new ColorDrawable(0xff1d97dd));

让我知道它是否有效:)

您可以使用 Android Asset Studio 创建自定义 ActionBar 样式,然后选择您希望 AB 具有的颜色。

您也可以尝试此 Whosebug post How do I change the background color of the ActionBar of an ActionBarActivity using XML?

中的解决方案

引用:

Just in case, if you target for minimum API level 11 , you can change ActionBar's background color by defining custom style, as:

<resources>
    <style name="MyTheme" parent="@android:style/Theme.Holo.Light">
        <item name="android:actionBarStyle">@style/MyActionBar</item>
    </style>

    <style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
        <item name="android:background">ANY_HEX_COLOR_CODE</item>
    </style>
</resources>

And, set "MyTheme" as theme for application / activity.

To change the action bar background, create a custom theme for your activity that overrides the actionBarStyle property. This property points to another style in which you can override the background property to specify a drawable resource for the action bar background.

When supporting Android 3.0 and higher only, you can define the action bar's background like this:

res/values/themes.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- the theme applied to the application or activity -->
    <style name="CustomActionBarTheme"
           parent="@android:style/Theme.Holo.Light.DarkActionBar">
        <item name="android:actionBarStyle">@style/MyActionBar</item>
    </style>

    <!-- ActionBar styles -->
    <style name="MyActionBar"
           parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
        <item name="android:background">@drawable/actionbar_background</item>
    </style>
</resources>

Then apply your theme to your entire app or individual activities:

AndroidManifest.xml

<application android:theme="@style/CustomActionBarTheme" ... />

Source

您可以在 styles.xml 中设置自定义颜色。

<resources>
  <!-- inherit from the material theme -->
  <style name="AppTheme" parent="android:Theme.Material">
    <!-- Main theme colors -->
    <!--   your app branding color for the app bar -->
    <item name="android:colorPrimary">@color/primary</item>
    <!--   darker variant for the status bar and contextual app bars -->
    <item name="android:colorPrimaryDark">@color/primary_dark</item>
    <!--   theme UI controls like checkboxes and text fields -->
    <item name="android:colorAccent">@color/accent</item>
  </style>
</resources>

然后创建一个 colors.xml 文件并以十六进制格式在那里设置颜色。

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="primary">#F44336</color>
    <color name="primaryDark">#D32F2F</color>
    <color name="accent">#448AFF</color>
</resources>

去这里了解更多:https://developer.android.com/training/material/theme.html#ColorPalette