2014-10-18 127 views
26

我以前用actionBarStyle更改了AppCompat狀態欄的顏色,並用背景創建了一個我想要的顏色。更改材質設計AppCompat ActionBar顏色

現在,使用Material Design AppCompat,此方法不再有效。

你能幫我嗎?謝謝。

+0

你可能指的是動作條?只是爲了清楚意味着什麼。 – 2014-10-18 02:26:34

+0

@NikolaDespotoski我有一個ActionBarActivity必須得到AppCompat風格。所以,我所做的就是向AppCompat樣式添加一個名爲actionBarStyle的屬性,並且我在那裏設置的樣式具有將Action條變爲另一種顏色的背景。 – user3184899 2014-10-18 02:30:50

+0

@NikolaDespotoski有幫助嗎? – user3184899 2014-10-18 19:26:11

回答

90

有一個新屬性,稱爲colorPrimary,你可以在你的主題中定義。這會給你ActionBar或工具欄一個純色。

下面一個小例子:

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

請注意:它必須是唯一的colorPrimary,不android:colorPrimary,在每一個價值觀文件夾除了values-v21之一。

您可以在developer.android.com上閱讀更多關於自定義調色板的信息。

+1

顏色主要用於Api 20+。在奇巧上時,這對我來說毫無用處。 – 2014-10-19 14:51:01

+8

不是。 'colorPrimary'是'v7-appcompat'的一部分,所以它基本上可用於API-Level 7及更高版本(請注意父'Theme.AppCompat')。 – reVerse 2014-10-19 15:02:28

+1

它的工作!謝謝! – user3184899 2014-10-19 15:13:34

3

在我的情況下,@reVerse有一個部分答案。爲了讓colorPrimary正常工作,我不得不做一些額外的修改,因爲我正在自定義工具欄的其他部分......特別是文本顏色。

這是我的工作styles.xml文件,與表明的意見我在做什麼錯:

<!-- As @reVerse mentioned, you need to use colorPrimary instead of android:colorPrimary --> 
<style name="MyTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 
    <item name="colorPrimary">@color/my_toolbar_color</item> 
</style> 

<!-- I was incorrectly using @style/Theme.AppCompat.Light for the popupTheme attribute --> 
<style name="MyToolbarStyle" parent="Widget.AppCompat.ActionBar"> 
    <item name="theme">@style/MyToolbarTextStyle</item> 
    <item name="popupTheme">@style/ThemeOverlay.AppCompat.Light</item> 
</style> 

<!-- I was incorrectly using Them.AppCompat.Light for the parent here --> 
<style name="MyToolbarTextStyle" parent="ThemeOverlay.AppCompat.Dark.ActionBar"> 
    <item name="android:textColorPrimary">@color/my_toolbar_text_color</item> 
</style> 

因此,定製比工具欄背景顏色更多的時候,你需要確保使用的一個ThemeOverlay主題,無論出於何種原因,colorPrimary都將被忽略。

爲了完整起見,這裏是我用我的工具欄佈局文件,Toolbar.xml:

<android.support.v7.widget.Toolbar 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schmeas.android.com/apk/res-auto" 
    style="@style/MyToolbarStyle" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" /> 

希望這可以幫助別人!

0
<style name="AppTheme" parent="Theme.AppCompat.Light"> 

     <item name="colorPrimary">@color/ColorPrimary</item> 
     <item name="colorPrimaryDark">@color/ColorPrimaryDark</item> 
     <!-- Customize your theme here. --> 
    </style> 

另請參見:Making Custom ActionBar

0

directions in the documentation設置應用欄建議您在設定的主題在Android清單文件是這樣的:

<application 
    android:theme="@style/Theme.AppCompat.Light.NoActionBar" 
    /> 

然而,在我的經驗,這凌駕於定製AppTheme的colorPrimary風格。什麼工作對我來說是定義自定義主題是這樣的:

<style name="AppTheme" parent="Theme.AppCompat.NoActionBar"> 
    <!-- Customize your theme here. --> 
    <item name="colorPrimary">@color/colorPrimary</item> 
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
    <item name="colorAccent">@color/colorAccent</item> 
</style> 

然後在AndroidManifest文件中使用這個主題。

<application 
    android:theme="@style/AppTheme" 
    /> 

後基於colorPrimary活動,設置工具欄背景顏色的罰款。

<android.support.v7.widget.Toolbar 
    android:id="@+id/my_toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="?attr/actionBarSize" 
    android:background="?attr/colorPrimary" 
    android:elevation="4dp" 
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> 

(設置ThemeOverlay與黑暗的主題,確保該文本是光。)