Android操作栏没有反映颜色变化

时间:2015-09-01 03:14:24

标签: android-studio android-actionbar android-theme

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

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

2 个答案:

答案 0 :(得分:1)

您正在更改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来编译项目。

答案 1 :(得分:0)

以下似乎已经成功了。我在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:标签被背景颜色覆盖,所以我添加了

<item name="android:displayOptions">showTitle</item>
<item name="displayOptions">showTitle</item>
相关问题