4

我有一個「光」爲主題的應用程序:程序兼容性工具欄:從上面的主題設置工具欄主題

<style name="Theme.MyTheme" parent="Theme.AppCompat.Light.NoActionBar"> 
    <item name="colorPrimary">@color/primary</item> 
    <item name="colorPrimaryDark">@color/primary_dark</item> 
    <item name="colorAccent">@color/accent</item> 
    <!-- etc. --> 
</style> 

我希望我的Toolbar s到暗的主題,所以我必須設置下面的樣式,就像通過Chris Banes建議:

<style name="Theme.MyTheme.Toolbar" parent="ThemeOverlay.AppCompat.Dark.ActionBar"> 
    <!-- stuff --> 
</style> 

然後,加入android:theme="@style/Theme.ByodTheme.Toolbar"android.support.v7.widget.Toolbar,一切正常(即使在Android Studio預覽不顯示白顏色的標題,它的工作原理上的設備):

Regular application of theme

現在,而不是在我的應用程序指定android:theme每一個工具欄,我想指定樣式在我的主旋律,而忘記了它。我曾嘗試toolbarStyle屬性,但它看起來像按預期這是行不通的,因爲它完全打亂了標準屬性:

Attempt with toolbarStyle

我也通過將工具欄從主題繼承Widget.AppCompat.Toolbar作出的其他嘗試,並通過更改titleTextAppearancesubtitleTextAppearance,但然後它看起來不可能更改溢出圖標顏色(是的,我試圖設置actionOverflowButtonStyle並從Widget.AppCompat.ActionButton.Overflow繼承該樣式,沒有成功更改溢出圖標顏色)。

有沒有一種方法可以從單一點指定應用程序中每個工具欄的主要主題?

回答

2

您可以使用自定義屬性 - 基本上只需設置您的工具欄使用您將在主題中定義的自定義值。

<android.support.v7.widget.Toolbar 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="?attr/actionBarSize" 
    android:background="?attr/colorPrimary" 
    android:theme="?attr/toolbar_theme" 
    /> 

重要的部分是android:theme="?attr/toolbar_theme",它引用了當前主題的自定義屬性。

您需要的地方聲明這個屬性,像這樣:

<resources> 
    <attr name="toolbar_theme" format="reference" /> 
</resources> 

然後你就可以定義工具欄,要在你的主題值分配給toolbar_theme使用的值。例如,以使其使用Theme.MyTheme.Toolbar你可以像這樣定義它:

<style name="Theme.MyTheme" parent="Theme.AppCompat.Light.NoActionBar"> 
    <item name="colorPrimary">@color/primary</item> 
    <item name="colorPrimaryDark">@color/primary_dark</item> 
    <item name="colorAccent">@color/accent</item> 
    <!-- etc. --> 
    <item name="toolbar_theme">@style/Theme.MyTheme.Toolbar</item> 
</style> 

有關使用屬性的好處是,它可以讓你的應用程序有多個主題,以及單個工具欄的資源會使用任何價值,你」主題內設置。

0

如果我正確地理解了這個問題,您想要設置您的應用程序的主題一次,並將所有樣式應用於每個活動/片段中的所有工具欄。

如果是的話,我會推薦這種方法:

<style name="MyTheme" parent="Theme.AppCompat.NoActionBar"> 
    <item name="android:textColorPrimary">@color/MyColorPrimaryText</item> 
    <item name="android:windowBackground">@color/MyColorWindowBackground</item> 
    <item name="colorPrimary">@color/MyColorPrimary</item> 
    <item name="colorPrimaryDark">@color/MyColorPrimaryDark</item> 
    <item name="colorAccent">@color/MyColorAccent</item> 
    <item name="colorControlNormal">@color/MyColorControlNormal</item> 
    <item name="colorControlHighlight">@color/MyColorControlHighlight</item> 
    <item name="toolbarStyle">@style/MyToolbar</item> 
    <item name="windowActionModeOverlay">true</item> 
    <item name="actionModeBackground">@color/MyColorPrimaryDark</item> 
</style> 

<style name="MyToolbar" parent="Widget.AppCompat.Toolbar"> 
    <item name="android:layout_width">match_parent</item> 
    <item name="android:layout_height">wrap_content</item> 
    <item name="android:minHeight">@dimen/MyToolbarMinHeight</item> 
    <item name="android:gravity">center_vertical</item> 
    <item name="android:background">@color/MyColorPrimary</item> 
    <item name="android:elevation" tools:ignore="NewApi">10dp</item> 
    <item name="theme">@style/MyToolbarTheme</item> 
</style> 

<style name="MyToolbarTheme" parent="ThemeOverlay.AppCompat.ActionBar"> 
    <item name="android:textColorPrimary">@color/MyColorPrimaryText</item> 
    <item name="colorControlNormal">@color/MyToolbarColorControlNormal</item> 
    <item name="colorControlHighlight">@color/MyToolbarColorControlHighlight</item> 
</style> 

然後在你的清單:

<application 
    android:name=".MyApp" 
    android:theme="@style/MyTheme"> 
    <activity android:name=".MyActivity1"/> 
    <activity android:name=".MyActivity2"/> 
</application> 

然後在你的活動佈局文件,包括工具欄有:

style="?attr/toolbarStyle" 

因此,myActivity1.xml佈局文件應如下所示:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <android.support.v7.widget.Toolbar 
    android:id="@+id/appBar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    style="?attr/toolbarStyle" /> 

<!-- ETC. --> 

</LinearLayout> 

,並注意改變導航後退按鈕顏色或溢出按鈕的顏色,您可以設置:

<item name="colorControlNormal">@color/MyColorControlNormal</item> 
<item name="colorControlHighlight">@color/MyColorControlHighlight</item> 

和:

<item name="android:textColorPrimary">@color/MyColorPrimaryText</item> 

可用於更新動作條的文本顏色,但是您可以在工具欄主題中定義它。

希望這會有所幫助。