Android 操作栏不反映颜色变化

Android action bar not reflecting color change

我已经阅读了很多关于更改操作栏颜色的答案,但我似乎无法让它发挥作用。这是我拥有的:

styles.xml

<resources>

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

    <!-- ActionBar styles -->
    <style name="MyActionBar" parent="Theme.AppCompat.Light">
        <item name="android:windowBackground">@color/actionBarBackground
        </item>
    </style>
    </resources>

我也试过了

<item name="android:Background">#F33</item>

AndroidManifest.xml

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

您正在更改 android:windowBackground 而不是 ActionBar 的背景。

无论如何,从 AppCompat v21+ 开始,您可以使用一种新的方式来自定义您的 ActionBar。

  • 您的所有活动必须从 AppCompatActivity

  • 开始
  • 您的所有主题(需要 ActionBar/Toolbar)必须继承自 Theme.AppCompat。

只需使用这样的主题:

<style name="Theme.MyTheme" parent="Theme.AppCompat.Light">
    <!-- Set AppCompat’s color theming attrs -->
    <item name="colorPrimary">@color/my_awesome_red</item>
    <item name="colorPrimaryDark">@color/my_awesome_darker_red</item>

    <!-- The rest of your attributes -->
</style>

AppCompat当前版本为23,编译工程需要API23。

以下似乎已成功。我在这里找到了 Change Background color of the action bar using AppCompat

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

<!-- ActionBar styles -->
<style name="MyActionBar" parent="Theme.AppCompat.Light">
    <item name="android:background">@color/actionBarBackground</item>
    <item name="background">@color/actionBarBackground</item>
</style>

但是,现在我的清单文件中的 android:label 被背景色覆盖了,所以我添加了

<item name="android:displayOptions">showTitle</item>
<item name="displayOptions">showTitle</item>