添加两个具有不同主题的AppCompat工具栏

tok*_*udu 5 android android-5.0-lollipop android-toolbar

我想在我的应用程序中有两个不同的android.support.v7.widget.Toolbars,一个黑暗,一个灯,并在需要时在它们之间切换.但是,当我在第二个工具栏上设置不同的主题时,它似乎也重置了第一个工具栏上的主题.

这是一个错误或预期的行为?

工具栏1:

<android.support.v7.widget.Toolbar
xmlns:sothree="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_toolbar"
android:layout_height="?attr/actionBarSize"
android:layout_width="match_parent"
android:layout_gravity="top"
android:background="@drawable/action_bar_background"
sothree:theme="@style/ThemeOverlay.AppCompat.ActionBar"
sothree:contentInsetStart="0dp"/>
Run Code Online (Sandbox Code Playgroud)

工具栏2:

<android.support.v7.widget.Toolbar
xmlns:sothree="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/clip_toolbar"
android:layout_height="?attr/actionBarSize"
android:layout_width="match_parent"
android:layout_gravity="top"
android:background="@android:color/transparent"
sothree:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
sothree:popupTheme="@style/ThemeOverlay.AppCompat.Light"
sothree:contentInsetStart="0dp"/>
Run Code Online (Sandbox Code Playgroud)

Kar*_*uri 0

制作一个 ContextThemeWrapper:

ContextThemeWrapper wrapper = new ContextThemeWrapper(context, R.style.Some_Theme);
Run Code Online (Sandbox Code Playgroud)

使用当前 Context 来扩充一个工具栏,并使用 ContextThemeWrapper 来扩充另一个工具栏。用于getLayoutInflater()获取每个上下文的充气器。