2014-10-19 62 views
2

因此,我是新应用开发人员,目前正在遵循android.com上的开发人员指南。我卡上的“风格化的动作条你”的一部分,因为代码暗示的网站上不起作用:actionBar背景不变

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

     <!-- Support library compatibility --> 
     <item name="actionBarStyle">@style/MyActionBar</item> 
    </style> 

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

     <!-- Support library compatibility --> 
     <item name="background">@drawable/actionbar_background</item> 
    </style> 
</resources> 

我得到关于Android的兼容性错误:backgound不能因为我的minSdk是使用7(虽然指南声称它应该工作)。 我不想更改我的minSdk,我尝试将themes.xml分成2个,一个放入值中,另一个放入值-V11中。该错误不再发生,但是当我运行该应用程序时,actionBar不会更改其背景。这里有两个文件:

这是一个价值观

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

     <!-- Support library compatibility --> 
     <item name="actionBarStyle">@style/MyActionBar</item> 
    </style> 
    <style name="MyActionBar" 
      parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse"> 
     <!-- Support library compatibility --> 
     <item name="background">@drawable/actionbar_background</item> 
    </style> 
</resources> 

而这一次是在价值观-11

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

你有什么想法,这里有什么问题吗?

设备上的API LVL为21

回答

2

作为AppCompat v21,所述android:属性是不再使用。它还支持color theming,它提供了一个非常简单的方法来设置操作栏(Android 5.0设备上的状态栏)的颜色,使用一个非常简单的主题:

<style name="Theme.MyTheme" parent="Theme.AppCompat.Light"> 
    <!-- colorPrimary is used for the default action bar background --> 
    <item name="colorPrimary">@color/my_awesome_color</item> 

    <!-- colorPrimaryDark is used for the status bar --> 
    <item name="colorPrimaryDark">@color/my_awesome_darker_color</item> 

    <!-- colorAccent is used as the default value for colorControlActivated 
     which is used to tint widgets --> 
    <item name="colorAccent">@color/accent</item> 

    <!-- You can also set colorControlNormal, colorControlActivated 
     colorControlHighlight & colorSwitchThumbNormal. --> 
</style>