PopupWindow与导航抽屉重叠

时间:2020-01-16 14:04:24

标签: android popup popupwindow

当前,我的弹出窗口与其他视图重叠。 setElevation(0)不变。 setOverlapAnchor(false)setAttachedInDecor(true)也无济于事。以下是我使用的代码。我需要将弹出窗口置于导航抽屉下

  private fun showPopup(anchorView: View) {
        PopupWindow(
            LayoutInflater.from(activity).inflate(
                R.layout.popup_layout,
                null
            ),
            100,
            100,
            false
        )
       .apply {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    view?.elevation = 0f
                    contentView.elevation = 0f
                    elevation = 0f
                }
                isTouchable = false
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
                    isAttachedInDecor = true
                }

                PopupWindowCompat.setOverlapAnchor(this, false)
                PopupWindowCompat.showAsDropDown(this, anchorView, 0, 0, Gravity.NO_GRAVITY)
            }
    }

enter image description here

1 个答案:

答案 0 :(得分:3)

PopupWindow是一个窗口。您的导航抽屉位于具有自己的视图层次结构的另一个窗口上。

是这样的:

-活动
---- window1
------视图层次结构
-------- NavigationDrawer
---- window2
------弹出

使用PopupWindow无法实现所需的内容。

一种可能的解决方法是在打开和关闭导航时隐藏并显示弹出窗口。这是回调:

https://developer.android.com/reference/android/support/v4/widget/DrawerLayout.DrawerListener.html#ondrawerstatechanged

或者您可以自己将视图添加为弹出窗口,并注意位置和重力。

最后但并非最不重要的一点,请检查这些库,因为它们可能具有所需的内容。它们使用视图,因此您可以按自己的方式进行管理。

https://github.com/sephiroth74/android-target-tooltip

https://github.com/tomergoldst/tooltips