如果我理解问题正确的话,你希望只设置一次应用程序的主题,并将所有样式应用于每个活动/片段中的所有工具栏。
如果是这样的话,我建议采用以下方法:
<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" />
</LinearLayout>
请注意,要更改导航返回按钮颜色或溢出按钮颜色,您可以设置以下内容:
<item name="colorControlNormal">@color/MyColorControlNormal</item>
<item name="colorControlHighlight">@color/MyColorControlHighlight</item>
并且:
<item name="android:textColorPrimary">@color/MyColorPrimaryText</item>
可以用于更新操作栏文本颜色,但需要在工具栏主题中定义。
希望这能帮到你。