2014-10-19 61 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>