如何更改 Android 操作栏的颜色?

How can I change the color of the Android Action Bar?

我正在尝试更改 Android ActionBar 的颜色,但我的应用程序每次都因错误而关闭。我已经尝试了其他帖子中的所有其他建议和修复,但它们似乎对我不起作用。另外 - minSdkVersion 19

错误:

03-17 11:25:56.884  11999-11999/ca.holdfastonline.menu_test_02 E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: ca.holdfastonline.menu_test_02, PID: 11999
    java.lang.RuntimeException: Unable to start activity ComponentInfo{ca.holdfastonline.menu_test_02/ca.holdfastonline.menu_test_02.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)

我的Styles.xml

<resources>
    <style name="QueryTheme" parent="@style/Theme.AppCompat">
        <!-- Any customizations for your app running on devices with Theme.Holo here -->
    </style>
    <!-- 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">#2980b9</item>
    </style>
</resources>

我的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="ca.holdfastonline.menu_test_02" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/QueryTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/CustomActionBarTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

谢谢。

java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

你只有一个activity。它正在使用 @style/CustomActionBarTheme。您对 CustomActionBarTheme 的定义继承自 Theme.Holo.Light.DarkActionBar。那不是 AppCompat 主题。将 Theme.Holo.Light.DarkActionBar 更改为 Theme.AppCompat.Light.DarkActionBar

这可能是因为在您的 Java 文件中您正在使用 ActionBarActivity 而您应该使用 Activity.

更多信息 基本上你使用的是 ActionBarActivity,这个 activity 需要一个 Theme.AppCompact,这是你的错误来源。要解决此问题,您需要更改为使用常规 Activity 或仅使用 Theme.AppCompact