使用深色背景时棒棒糖弹出菜单上的伪影

Artifacts on lollipop popup menu when using dark background

如您所见,弹出窗口后面的背景变形了。它只发生在棒棒糖上!请不要对我的样式文件中的所有 x_ 感到困惑。我刚刚取消了代码的品牌化。

在主题文件中:

 <style name="core" parent="Theme.AppCompat.Light">

    <!-- Popup menu -->
    <item name="android:popupMenuStyle">@style/x_popup_menu_theme</item>
    <item name="android:textAppearanceSmallPopupMenu">@style/x_popup_menu_small_text</item>
    <item name="android:textAppearanceLargePopupMenu">@style/x_popup_menu_large_text</item>

</style>

在 v21/themes_styles.xml

<style name="x_popup_menu_theme" parent="Widget.AppCompat.PopupMenu">
    <item name="android:popupBackground">@color/x_navy_dark</item>
</style>

<style name="x_popup_menu_large_text" parent="TextAppearance.AppCompat.Widget.PopupMenu.Large">
    <item name="android:textColor">@color/std_white</item>
</style>

<style name="x_popup_menu_small_text" parent="TextAppearance.AppCompat.Widget.PopupMenu.Small">
    <item name="android:textColor">@color/std_white</item>
</style>

如果我change/remove这样的样式,它的工作原理如下图所示。

<style name="x_popup_menu_theme" parent="Widget.AppCompat.PopupMenu">
</style>

<style name="x_popup_menu_large_text" parent="TextAppearance.AppCompat.Widget.PopupMenu.Large">
</style>

<style name="x_popup_menu_small_text" parent="TextAppearance.AppCompat.Widget.PopupMenu.Small">
</style>

[已编辑 - 删除了建议关闭硬件加速的原始答案]

这似乎是 android 5.0 中帧缓冲区的错误。它已在 android 5.1 中修复。 Bug listing

这是来自 Chet Haase 的关于如何解决该问题的消息:

The trigger is that you're setting the background of the popup window to a color, which resolves to a colored rectangle. You should either be using a rounded rect (like the default style uses) or, better yet, use the dark theme instead of a custom color. The bug [in android] (which is [fixed in 5.1]) was that we were not accounting for the padding for shadows and were not setting the correct translucency flags in the window in this situation.

现在我只是使用线性布局创建了自己的弹出菜单并手动处理点击和后退按钮。不太优雅,但它比弹出菜单更符合美术团队的要求。

           <LinearLayout
                android:id="@id/fake_popup"
                android:layout_below="@id/pending"
                android:visibility="gone"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" android:orientation="vertical">
                <TextView
                    style="@style/button.dropdown"
                    android:id="@id/send_reminder"
                    android:background="@color/navy_dark"
                    android:textColor="@color/std_white"
                    android:text="@string/send_reminder"/>
                <TextView
                    style="@style/button.dropdown"
                    android:id="@id/withdraw_invite"
                    android:background="@color/navy_dark"
                    android:textColor="@color/std_white"
                    android:text="@string/withdraw_invite"/>

            </LinearLayout>

处理对 show/hide 菜单的点击(注意注释是 ButterKnife):

    @OnClick (R.id.pending) public void showPendingActionsMenu () {
    // pendingActionsPopupMenu.show ();

    if (pendingPopup.isShown ()) {
        hidePendingPopup ();
    } else {
        showPendingPopup ();
    }
}

并处理各个选项的点击:

@OnClick (R.id.send_reminder)
public void sendReminder () { //your code here }

不要忘记覆盖 onBackPressed!

@Override public void onBackPressed () {
    if (pendingPopup.isShown ()) {
        hidePendingPopup ();
    } else {
        super.onBackPressed ();
    }
}

Android 5.0.x(已在 5.1 中修复)中存在错误,其中在升高的 [=26= 中设置不透明(例如 @color)背景] 导致视觉伪影。

作为 5.0.x 设备上的解决方法,您可以将背景设置为非不透明的可绘制对象,例如圆角矩形。

res/drawable/my_popup_bg.xml:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">
    <corners android:radius="2dp" />
    <solid android:color="@color/x_navy_dark" />
</shape>

res/values/styles.xml:

...

<style name="x_popup_menu_theme" parent="Widget.AppCompat.PopupMenu">
    <item name="android:popupBackground">@drawable/my_popup_bg</item>
</style>