android - AppCompat 样式背景传播到 ToolBar 内的 Image

标签 android android-appcompat android-toolbar

上下文

我正在使用最新的 AppCompat v7 lib (21.0.0),并且我已将我的应用从 ActionBar 迁移到 ToolBar

问题

  1. 工具栏中的图像自动接收与工具栏相同的背景

目前 SearchBox 是在操作栏上设置的自定义 View (来自以前的实现)我计划切换到 SearchView 并相应地设置它的样式,但我仍然对我现在面临的问题非常感兴趣。

Img background

  1. 当长按工具栏中的菜单项时,会出现带有提示文本的 toast,并且文本的背景与工具栏相同。

Toast background

我怎样才能避免这种情况?

这是我的工具栏布局以及样式和主题

布局 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上设置透明背景

android:background="@color/transparent"

这并不能解决 toast 问题。

解决这两个问题

我为工具栏设置了背景

<?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>

正如 Chris Banes 所指出的,可以为工具栏创建一种样式,而不是声明 android:background。

这解决了我的问题,但我似乎还有另一个问题,即没有应用 popupTheme 并且我的 ShareActionMenuItem 下拉菜单没有带有黑色文本的白色背景。 在此处查看另一个问题:AppCompat ToolBar popupTheme not used in the ShareAction MenuItem

也许这是 appcompat 库中的一个错误。

最佳答案

你不应该这样使用 theme:

  • style = 工具栏的本地样式
  • theme = 对工具栏中膨胀的所有内容都是全局的

解决方法 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>

关于android - AppCompat 样式背景传播到 ToolBar 内的 Image,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26490126/

相关文章:

android - 什么是 ScrimInsetsFrameLayout?

android - CollapsingToolBarLayout - 状态栏稀松布颜色不会改变

Java,MalformedChunkCodingException

java - Gson 在 "$Types.resolve"上溢出并出现 StackOverflow 异常

Android:AppCompat Snackbar 和非 AppCompat FloatingActionButton

android - 使用支持库删除操作栏下方的阴影和线条

android - 如何更改微调器中的下拉高度

android - 将 Android App Bundle 上传到 Google Play 控制台 - key 签名错误

android - 在android中的依赖列表appcompat中发现了2个版本的android-support-v4.jar

java - PreferenceActivity 中的工具栏设置