2015-09-01 28 views
2

我已经阅读了许多关于更改操作栏颜色的答案,但似乎无法使其工作。下面是我有:Android操作栏不能反映颜色变化

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

回答

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

当前版本程序兼容性是23.这需要API23编译项目。

+0

我所有的活动已经扩展'AppCompatActivity' 您发布的代码似乎与我拥有的代码完全相同,除非我错过了某些内容。 我在清单本身的活动中添加了我的样式,并在应用程序调用该活动后更改了背景,但只适用于活动。应该有一个活动栏的活动?我错过了一个清晰的概念。 – Cliff

+0

@Cliff我不知道你是否在你的布局中使用工具栏。在这种情况下发布你的代码。否则,操作栏的颜色由活动使用的主题中的colorprimary attrr定义。在你的主题中,我没有看到它。 –

+0

in menu_main我有 '

<项机器人:ID =” @ + ID/ACTION_SEARCH” 机器人:图标= “@绘制/ search_50” 机器人:标题= “@串/ ACTION_SEARCH” 应用程式:showAsAction = “ifRoom”/> <项目机器人:ID = “@ + ID/action_settings” 机器人:标题= “@字符串/ action_settings” 机器人:showAsAction = “从不”/> ' – Cliff