Android AppCompat样式背景已传播到工具栏内的图像
上下文 我正在使用最新的AppCompat v7库(21.0.0),我已经将我的应用程序从ActionBar迁移到了工具栏 问题Android AppCompat样式背景已传播到工具栏内的图像,android,android-appcompat,android-toolbar,Android,Android Appcompat,Android Toolbar,上下文 我正在使用最新的AppCompat v7库(21.0.0),我已经将我的应用程序从ActionBar迁移到了工具栏 问题 工具栏中的图像自动接收与工具栏相同的背景 目前,SearchBox是在操作栏上设置的一个自定义视图(来自以前的实现),我计划切换到SearchView并相应地设置它的样式,但我仍然对我现在面临的问题非常感兴趣 长按工具栏中的某个菜单项时,将显示一个带有提示文本的土司,并且该文本的背景与工具栏相同 我怎样才能避免这种情况 这是我的工具栏布局以及样式和主题 layou
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/my_awesome_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/MyActionBarStyle"/>
这并不能解决吐司问题
两个问题的解决方案
我为工具栏设置了背景
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/my_awesome_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/green"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/MyActionBarStyle"/>
这可能是appcompat库中的一个bug。您不应该像这样使用主题:
- 样式=工具栏的本地样式
- 主题=工具栏中所有内容的全局
解决方案2,正如您所说的,是一种正确的方法。如果要将某些值提取到样式中,可以这样做:
<android.support.v7.widget.Toolbar
android:id="@+id/my_awesome_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
style="@style/MyDarkToolbar" />
风格:
<style name="MyDarkToolbarStyle" parent="Widget.AppCompat.Toolbar">
<item name="android:background">@color/green</item>
<item name="popupTheme">@style/ThemeOverlay.AppCompat.Light</item>
<item name="theme">@style/ThemeOverlay.AppCompat.Dark.ActionBar</item>
</style>
@颜色/绿色
@style/ThemeOverlay.AppCompat.Light
@style/ThemeOverlay.AppCompat.Dark.ActionBar
切勿使用主题进行编辑。因为主题定义了整个应用程序行为。你可以使用两种方法
方法:1
您可以直接在appbar.xml中指定参数,如下所示
在appbar.xml中
方法:2
您可以使用Chris_Banes在上述帖子中建议的样式
在appbar.xml中:
风格:
@颜色/绿色
@style/ThemeOverlay.AppCompat.Light
@style/ThemeOverlay.AppCompat.Dark.ActionBar
注意事项
使用新工具栏,您可以应用样式和主题。他们是
不一样!该样式是工具栏视图的本地样式,例如
背景色。app:theme是所有ui元素的全局主题
在工具栏中充气,例如标题和图标的颜色
您可以尝试第二种解决方法。它解决了这个问题,但是如果你想要黑暗的ActionBar和光明的DropDown,那么我需要为真实的ActionBar(使用时)和工具栏(需要时)管理两个不同的主题?不,你摆脱了旧的ActionBar,你现在只使用工具栏。这是否意味着我现在需要为每个活动添加一个工具栏(通过XML或java)?感觉有点奇怪是的,把它放在自己的xml布局中我知道我以前用过的样式但是你在博客上写的“app:theme=“@style/ThemeOverlay.AppCompat.Dark.ActionBar”app:poputheme=“@style/ThemeOverlay.AppCompat.Light”是什么意思?我做了很多测试,变通方法2要好得多,是的,正如你所指出的,使用一种风格会让它变得更好。另请看我的另一个问题,因为它与流行主题有关。克里斯,谢谢你解释“风格”和“主题”之间的区别。theme.AppCompat和ThemeOverlay.AppCompat之间的区别是什么?我尝试了很多方法,其中大多数都是一样的,但这是唯一对我有效的方法。谢谢
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/my_awesome_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/green"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/MyActionBarStyle"/>
<style name="MyActionBarStyle" parent="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<item name="android:windowActionBarOverlay">true</item>
<!-- Support library compatibility -->
<item name="windowActionBarOverlay">true</item>
</style>
<android.support.v7.widget.Toolbar
android:id="@+id/my_awesome_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
style="@style/MyDarkToolbar" />
<style name="MyDarkToolbarStyle" parent="Widget.AppCompat.Toolbar">
<item name="android:background">@color/green</item>
<item name="popupTheme">@style/ThemeOverlay.AppCompat.Light</item>
<item name="theme">@style/ThemeOverlay.AppCompat.Dark.ActionBar</item>
</style>