2015-06-20 36 views
1

我剛剛開始自己​​學習Android開發,而且我面臨一個問題,我需要幫助。Android:ActionBar Customization的問題

我在學習ActionBar。我有興趣定製ActionBar

我跟着改變其背景顏色和物品的間距的步驟是:

  1. 當時的想法是有一個ActionBar一個基本主題。然後定製它。代碼:

    <!-- Base application theme. --> 
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 
        <!-- Customize your theme here. --> 
        <item name="android:actionBarStyle">@style/MyActionBarTheme</item> 
    </style> 
    
    <style name="MyActionBarTheme" parent="@android:style/Widget.Holo.Light.ActionBar"> 
        <item name="android:background">#F44336</item> 
    </style> 
    

  2. 接下來,我AndroidManifest.xml我用我創建了自定義主題。代碼:

    <application 
        android:allowBackup="true" 
        android:icon="@mipmap/ic_launcher" 
        android:label="@string/app_name" 
        android:theme="@style/AppTheme" > 
        <activity 
         android:name=".MainActivity" 
         android:label="@string/app_name" > 
         <intent-filter> 
          <action android:name="android.intent.action.MAIN" /> 
    
          <category android:name="android.intent.category.LAUNCHER" /> 
         </intent-filter> 
        </activity> 
    </application> 
    

但我ActionBar的顏色仍然是黑色的。我究竟做錯了什麼?我錯過了什麼嗎?

enter image description here

回答

3

您正在使用appcompat-v7操作欄反向移植。要設置操作欄的顏色,請在主題中使用colorPrimary,而不是android:background

例如,這個主題設置的三大色彩濃淡:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <style name="Theme.Apptheme" parent="Theme.AppCompat"> 
    <item name="colorPrimary">@color/primary</item> 
    <item name="colorPrimaryDark">@color/primary_dark</item> 
    <item name="colorAccent">@color/accent</item> 
    </style> 
</resources> 

這個特殊的樣本情況來定義顏色通過顏色資源值本身,在res/values/colors.xml文件:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <color name="primary">#3f51b5</color> 
    <color name="primary_dark">#1a237e</color> 
    <color name="accent">#ffee58</color> 
</resources> 

的使用自定義主題的活動然後獲取主要顏色作爲操作欄背景:

appcompat-v7 Sample

(來自this sample project

+0

謝謝。我仍然有一個問題。這將幫助我控制顏色,但是如何控制ActionBar的其他屬性。比如圖標之間的距離? – HaggarTheHorrible

+0

@HaggarTheHorrible:理想情況下,你只需在圖標之間留下距離即可。除此之外,「其他屬性」可以通過主題設置或通過從getSupportActionBar()獲得的'ActionBar'調用來控制。 – CommonsWare