AppCompat样式背景传播到ToolBar中的Image

Ben*_*oit 25 android android-appcompat android-toolbar

上下文

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

问题

  1. ToolBar中的图像自动获得与ToolBar相同的背景

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

Img背景

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

吐司背景

我怎么能避免这个?

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

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"/>
Run Code Online (Sandbox Code Playgroud)

价值观/ styles.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>
Run Code Online (Sandbox Code Playgroud)

主题

<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>
Run Code Online (Sandbox Code Playgroud)

问题1的解决方法1

第一个问题的解决方法是在ImageView上设置透明背景

android:background="@color/transparent"
Run Code Online (Sandbox Code Playgroud)

这并不能解决问题.

解决这两个问题

我为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"/>
Run Code Online (Sandbox Code Playgroud)

而且我不再为主题设置背景了.

<style name="MyActionBarStyle" parent="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    <item name="android:windowActionBarOverlay">true</item>
    <!-- Support library compatibility -->
    <item name="windowActionBarOverlay">true</item>
</style>
Run Code Online (Sandbox Code Playgroud)

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

这解决了我的问题,但后来我似乎有另一个问题,即popupTheme未应用,而我的ShareActionMenuItem下拉列表没有带黑色文本的白色背景.请参阅此处的另一个问题:AppCompat ToolBar popupTheme未在ShareAction MenuItem中使用

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

Chr*_*nes 46

你不应该这样使用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" />
Run Code Online (Sandbox Code Playgroud)

样式:

<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>
Run Code Online (Sandbox Code Playgroud)


Ash*_*rma 5

切勿使用主题进行编辑.因为主题定义了整体应用行为.你可以使用两种方法

制法:1

你可以直接在appbar.xml中指定参数,如下所示

在appbar.xml中

  <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.Toolbar
        android:id="@+id/my_awesome_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/green"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme=">style/ThemeOverlay.AppCompat.Light"/>
Run Code Online (Sandbox Code Playgroud)

方法:2

您可以使用Chris_Banes在上面的帖子中建议的样式

在appbar.xml中:

<android.support.v7.widget.Toolbar
    android:id="@+id/my_awesome_toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    style="@style/MyDarkToolbar" />
Run Code Online (Sandbox Code Playgroud)

很有型:

<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>
Run Code Online (Sandbox Code Playgroud)

注意事项

使用新工具栏,您可以应用样式和主题.它们是不同的!样式是工具栏视图的本地样式,例如背景颜色.app:theme对工具栏中充气的所有ui元素都是全局的,例如标题和图标的颜色.