从溢出菜单 enter/exit 动画中删除额外的背景颜色矩形?

Remove extra background color rectangle from overflow menu enter/exit animations?

当我在我的应用程序中打开溢出菜单时,在整个 enter/exit 动画中,我看到菜单本身后面显示了一个菜单背景颜色的实心矩形。这是显示此内容的动画(速度减慢到 75%):

这个无关的彩色矩形的存在破坏了漂亮的 enter/exit 动画!如何在保留所有其他 Toolbar 样式的同时删除它?


研究

我在 SO 上阅读了很多关于 Toolbar 样式的答案,以及 Chris Banes 自己关于该主题的帖子。在过去几年中,style/theme 标签的使用似乎在每个 View 基础上发生了变化,这使得很难在任何地方找到明确的信息。

我使用支持库的版本 22.1.0 到 23.2.0(含)复制了此文件。


应用程序文件

styles.xml

<resources>

    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="ToolbarStyle" parent="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
        <item name="android:background">@color/colorPrimary</item>
    </style>

</resources>

activity_main.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:style="@style/ToolbarStyle" />

    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

运行时视图分析

作为@Commonsware ,我在 运行 时间查看了视图层次结构。编辑结果:

菜单已折叠(存在溢出按钮,没有神秘的额外矩形):

菜单扩展(溢出菜单视图单独显示Window):

比较两个菜单(稳定)状态时,主应用程序视图层次结构(如原始 Window 中所示)没有变化。因此,我的猜测是额外的矩形在菜单动画期间临时添加到主应用程序的 window 中,并在完成后立即删除。我不确定 为什么 会这样做 - 也许这是旧的(现在未使用的)显示和隐藏溢出菜单的方法的后遗症?如果我的推论是正确的,那么如果我们能够确定如何防止这种暂时的行为,那么这个问题就可以得到解决...

摘自 Chris Bane's answer to the question AppCompat style background propagated to the Image within the ToolBar

style = local to the Toolbar
theme = global to everything inflated in the Toolbar

根据上面的post 看来,您在android:theme 中设置的android:background 属性是动画期间额外背景颜色的原因。

我直接将 background 设置为 toolbar 并使用 android:colorBackground 属性设置弹出窗口的背景颜色。动画似乎工作正常。

代码:

activity_main.xml

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    android:theme="@style/CustomToolbarTheme" />

styles.xml

<style name="CustomToolbarTheme" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
    <item name="android:colorBackground">@color/colorPrimary</item>
    <item name="android:textColorPrimary">@android:color/white</item>
</style>