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

5

我希望在我的应用程序中有两个不同的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"/>

工具栏 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"/>
1个回答

0
创建一个ContextThemeWrapper:
ContextThemeWrapper wrapper = new ContextThemeWrapper(context, R.style.Some_Theme);

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


这在Lollipop上似乎很正常,但在Kitkat上失败了。有什么想法吗? - tokudu
不能在没有logcat的堆栈跟踪的情况下告诉你。 可能值得打开一个新问题。 - Karakuri
1
错误在支持库中:https://code.google.com/p/android/issues/detail?id=78289 而且这个解决方案不起作用。 - Michael

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接