PopupWindow 超出屏幕底部边界

PopupWindow goes out screen's bottom boundary

这是我在长按 GridView 项目时用来显示弹出窗口 window 的简化代码。

当最右边的项目弹出 window 调整到屏幕内。

但是当项目靠近屏幕底部时,弹出 window 被裁剪(部分在屏幕外)。

如何解决这个问题?

PopupWindow mDropDownMenu= new PopupWindow(list, WRAP_CONTENT, WRAP_CONTENT);

mDropDownMenu.showAsDropDown(aView);

aView 是 GridView 项。

文档说明了 showAsDropDown(View anchor)

 * Display the content view in a popup window anchored to the bottom-left
 * corner of the anchor view. If there is not enough room on screen to show
 * the popup in its entirety, this method tries to find a parent scroll
 * view to scroll. If no parent scroll view can be scrolled, the
 * bottom-left corner of the popup is pinned at the top left corner of the
 * anchor view.
 *

但它总是固定在左下角,而不是左上角。

找到解决方案。您必须测量视图并在 PopupWindow.

上设置测量
...
private fun showPopupWindow() {
        val popupView = layoutInflater.inflate(R.layout.standard_popup_window, null)
        popupView.measure(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        PopupWindow(popupView, popupView.measuredWidth, popupView.measuredHeight).apply {
            setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
            isTouchable = true
            isFocusable = true
            overlapAnchor = true
            width = popupView.measuredWidth
            height = popupView.measuredHeight
            contentView = popupView
            showAsDropDown(fragment_person_details__description)
        }
...

原来我必须设置下拉菜单高度来避免这个问题

List<DropDownListItem> items;

dropDown.setHeight( toPixels( 30 * items.size() ) );