2014-10-18 91 views
3

我想爲狀態欄着色,以獲得更好的Android 5.0版效果。我想要設置三種顏色的淺色,淺色和口音。主要暗色android API 21

但我最小的API是15.有沒有機會只使用API​​ lvl 21?或者我必須用min創建一個單獨的應用程序。 sdk 21?

編輯: 現在我得到我需要的一切,但statisbar顏色不會改變。

這是我的價值觀-V21/styles.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
<style name="AppBaseTheme" parent="android:Theme.Material.Light"> 
      <!-- API 21 theme customizations can go here. --> 
    <!-- Main theme colors --> 
    <!-- your app branding color for the app bar --> 
    <item name="android:colorPrimary">@color/primary</item> 
    <!-- darker variant for the status bar and contextual app bars --> 
    <item name="android:colorPrimaryDark">@color/primary_dark</item> 
    <!-- theme UI controls like checkboxes and text fields --> 
    <item name="android:colorAccent">@color/accent</item> 
     </style> 
</resources> 

這正常style.xml

<resources> 

    <!-- 
     Base application theme, dependent on API level. This theme is replaced 
     by AppBaseTheme from res/values-vXX/styles.xml on newer devices. 
    --> 
    <style name="AppBaseTheme" parent="android:Theme.Light"> 
     <!-- 
      Theme customizations available in newer API levels can go in 
      res/values-vXX/styles.xml, while customizations related to 
      backward-compatibility can go here. 
     --> 
    </style> 

    <!-- Application theme. --> 
    <style name="AppTheme" parent="AppBaseTheme"> 
     <!-- All customizations that are NOT specific to a particular API-level can go here. --> 
    </style> 

    <!-- the theme applied to the application or activity --> 
    <style name="CustomActionBarTheme" 
     parent="@android:style/Theme.Holo.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">@color/blue</item> 
    </style> 
</resources> 

任何想法,爲什麼這是不行的?

回答

8

你可以有不同的API層次不同的風格。

對於API < 21,您將使用正常的res/values/styles.xml,然後對於API 21+,您將擁有res/values-v21/styles.xml

如果設備運行API 21或更高版本,則它將使用-v21文件夾中的文件。如果您只想設置<color>值,則只需鍵入相同的名稱,然後按照colors.xml的方法進行操作。

http://developer.android.com/guide/topics/resources/providing-resources.html#AlternativeResources

實施例:

res/values/colors.xml

<!-- Colours for API <21 --> 
<color name="primaryDark">#800000</color> 
<color name="light">#800080</color> 
<color name="accent">#FF0000</color> 

res/values-v21/colors.xml

<!-- Colours for API 21+ --> 
<color name="primaryDark">#000008</color> 
<color name="light">#080008</color> 
<color name="accent">#0000FF</color> 
+0

謝謝!找不到任何關於它的事情。 – Nick 2014-10-30 19:30:30

+0

添加了我的XML文件。它不適合我。還有黑色的狀態欄。 – Nick 2014-11-01 09:21:40

+0

你是否指定了不同的'colors.xml'文件? – ipavl 2014-11-01 15:06:32

2

嘗試集編程背景:

actionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.actionbar_background)); 


actionbar_background.xml 

<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item> 
     <shape android:shape="rectangle"> 
      <solid android:color="#e59300" /> 
     </shape> 
    </item> 
    <item android:bottom="3dp"> 
     <shape> 
      <solid android:color="#fbb903" /> 
     </shape> 
    </item> 
</layer-list> 
1

colorPrimaryDark用於着色狀態BA r ...狀態欄主題是在Lollipop的Android中添加的功能。 請記住,舊設備上的狀態欄將爲黑色(無論主題指定了什麼內容)。

引用自「Android Programming Guide 2ed」的大書呆子牧場導遊P360。 但我無法在android開發人員指南中找到它。