2014-09-19 59 views
0

我在developer.android.com上學習文檔,在課程「樣式操作欄」,我遇到了問題。我需要更改背景的ActionBar,作爲here。我使用Android Action Bar Style Generator生成了所有圖像(用於背景,按鈕等),並將結果添加到我的項目的res/dir中。
它看起來像這樣: enter image description here無法自定義ActionBar

我手動添加文件RES /價值/的themes.xml,因爲它沒有創建項目後存在。 這裏是RES /值的代碼/的themes.xml:

<?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/ab_solid_example</item> 

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

,並應用這一主題,我加

機器人:主題= 「@風格/ CustomActionBarTheme」

用於應用程序的

以及AndroidManifest.xml中的2個活動。這裏是AndroidManifest.xml中的代碼:

<?xml version="1.0" encoding="utf-8"?> 

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/CustomActionBarTheme" > 
    <activity 
     android:name="com.example.mysecondapp.app.MainActivity" 
     android:label="@string/app_name" 
     android:theme="@style/CustomActionBarTheme"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <activity 
     android:name="com.example.mysecondapp.app.DisplayMessageActivity" 
     android:label="@string/title_activity_display_message" 
     android:parentActivityName="com.example.mysecondapp.app.MainActivity" 
     android:theme="@style/CustomActionBarTheme"> 
     <meta-data 
      android:name="android.support.PARENT_ACTIVITY" 
      android:value="com.example.mysecondapp.app.MainActivity" /> 
    </activity> 
</application> 

但是,結果沒有背景的變化。 前瞻:

enter image description here

如果我上運行模擬器這個程序,什麼也沒有發生過。

有誰知道如何解決它?

回答

1

你應該嘗試這樣的事情

工作得很好,我

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


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

只是嘗試了這種代碼:

ActionBar bar = getActionBar(); // or MainActivity.getInstance().getActionBar() 
bar.setBackgroundDrawable(new ColorDrawable("YOUR COLOR")); 

不需要使用操作欄的樣式。這是你可以做到的最簡單的一個。

+1

感謝您對此事發表評論,但正如我說,我學習,我想了解從offcial機器人爲什麼我的榜樣文檔不起作用,因爲我完成了所有工作,因爲它是用文檔編寫的。 – Alexander 2014-09-19 11:41:29