AppCompat 样式背景传播到工具栏中的图像 [英] AppCompat style background propagated to the Image within the ToolBar
问题描述
背景
我使用的是最新的 AppCompat v7 lib (21.0.0) 并且我已经将我的应用从 ActionBar 迁移到 ToolBar
I'm using the newest AppCompat v7 lib (21.0.0) and I have migrated my app from ActionBar to ToolBar
问题
- ToolBar 中的图像自动接收与 ToolBar 相同的背景
目前 SearchBox 是在操作栏上设置的自定义视图(来自之前的实现)我计划切换到 SearchView 并相应地设置样式,但我仍然对我现在面临的问题非常感兴趣.
Currently the SearchBox is a custom view set on the action bar (From previous implementation) I plan to switch if to the SearchView and style it accordingly but I'm still very interested in the problem I'm facing right now.
- 当长按 ToolBar 中的菜单项时,会出现带有提示文本的 toast,并且该文本的背景与 ToolBar 相同.
我怎样才能避免这种情况?
How can I avoid this?
这是我的工具栏布局和样式 &主题
Here is my toolbar layout and the style & theme
布局 v_toolbar.xml
layout v_toolbar.xml
<?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
<style name="MyActionBarStyle" parent="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<item name="android:background">@color/green</item>
<item name="android:windowActionBarOverlay">true</item>
<!-- Support library compatibility -->
<item name="background">@color/green</item>
<item name="windowActionBarOverlay">true</item>
</style>
主题
<style name="MyTheme" parent="AppBaseTheme">
<!-- Here we setting appcompat’s actionBarStyle -->
<!-- <item name="actionBarStyle">@style/MyActionBarStyle</item> -->
<item name="windowActionBar">false</item>
<!-- ...and here we setting appcompat’s color theming attrs -->
<item name="colorPrimary">@color/green</item>
<item name="colorPrimaryDark">@color/green_dark</item>
</style>
问题 1 的解决方法 1
第一个问题的解决方法是在ImageView上设置透明背景
The workaround for the first problem is to set a transparent background on the ImageView
android:background="@color/transparent"
这不能解决吐司问题.
This does not solve the toast issue.
解决这两个问题
我为 ToolBar 设置了背景
I set a background to the ToolBar
<?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"/>
而且我不再设置主题背景.
And I'm not setting the background on the theme anymore.
<style name="MyActionBarStyle" parent="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<item name="android:windowActionBarOverlay">true</item>
<!-- Support library compatibility -->
<item name="windowActionBarOverlay">true</item>
</style>
正如 Chris Banes 所指出的,可以为工具栏创建样式而不是声明 android:background.
As pointed by Chris Banes, a style could be created for the toolbar instead of declaring the android:background.
这解决了我的问题,但后来我似乎有另一个问题,即没有应用 popupTheme 并且我的 ShareActionMenuItem 下拉菜单没有带有黑色文本的白色背景.在此处查看另一个问题:AppCompat ToolBar popupTheme not used in the ShareAction MenuItem
This fixes my problem but then I seem to have another issue with the fact that the popupTheme isn't applied and my ShareActionMenuItem drop down does not have a white background with black text. See another question here: AppCompat ToolBar popupTheme not used in the ShareAction MenuItem
也许这是 appcompat 库中的一个错误.
Maybe this is a bug in the appcompat library.
推荐答案
你不应该像那样使用 theme
:
You should not be using theme
like that:
- style = 工具栏本地
- theme = 工具栏中所有内容的全局化
解决方法 2,正如您所说,是一种正确的方法.如果你想将一些值提取到一个样式中,你可以这样做:
Workaround 2, as you call it, is kind of the correct way. If you want to extract some values into a style, you can do it as so:
<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>
这篇关于AppCompat 样式背景传播到工具栏中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!