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

stk*_*ent 5 android android-theme android-styles android-toolbar

当我在我的应用程序中打开溢出菜单时,我看到菜单背景颜色的实心矩形显示在菜单本身的整个进入/退出动画中.这是一个动画显示这个(减速到75%的速度):

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


研究

我已经阅读了Toolbar关于SO的样式的一堆答案,以及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>
Run Code Online (Sandbox Code Playgroud)

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

运行时视图分析

正如@Commonsware 建议的那样,我在运行时查看了视图层次结构.在结果中编辑:

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

折叠菜单时查看层次结构图

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

展开菜单时查看层次结构图

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

bli*_*ard 3

克里斯·贝恩 (Chris Bane)对“AppCompat 样式背景传播到工具栏中的图像”问题的回答摘录

样式 = 工具栏本地
主题 = 工具栏中所有内容的全局主题

根据上面的帖子,android:background您在 中设置的属性似乎android:theme是动画期间出现额外背景颜色的原因。

我将 直接设置backgroundtoolbar并使用该android:colorBackground属性设置弹出窗口的背景颜色。动画似乎效果很好。

代码:

活动主文件

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

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