弹出窗口超过了屏幕底部边界

3
这是我用来在GridView项上长按时显示弹出窗口的简化代码。
当最右边的项时,弹出窗口会调整到屏幕内。
但是,在靠近屏幕底部的项目上,弹出窗口被裁剪(一部分在屏幕外)。
如何解决这个问题?
PopupWindow mDropDownMenu= new PopupWindow(list, WRAP_CONTENT, WRAP_CONTENT);

mDropDownMenu.showAsDropDown(aView);

文档中关于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.
 *

但它总是固定在左下角,而不向左上方移动。


请提供截图以便更好地理解。 - Jatin
我有同样的问题。这里有一个旧帖子 https://dev59.com/_2cs5IYBdhLWcg3wHwXU,但是没有给出可行的解决方案。 - Bohsen
我也尝试在API 22和25上使用PopupWindowCompat。没有任何区别。问题仍然存在。 - Bohsen
如果您明确设置高度和宽度,它将起作用: PopupWindow(popupView, 100, 100).apply { setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT)) isTouchable = true isFocusable = true showAsDropDown(fragment_person_details__description) } - Bohsen
2个回答

4
原来我必须设置下拉菜单的高度以避免这个问题。
List<DropDownListItem> items;

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

1

找到了解决方案。您需要测量视图并将测量结果设置在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)
        }
...

谢谢Bohsen,但这是安卓代码吗?几乎所有的代码都没有定义,包括R.layout.standard_popup_window。 - Jack
同时,setOverlapAnchor(boolean) 方法需要 API 23 及以上版本支持,而我的目标版本为 API 16+。 - Jack
这是使用Android Kotlin扩展的Android Kotlin代码。只需删除setOverlapAnchor,不应该有影响。应该能够在没有它的情况下正常工作。 - Bohsen
R.layout.standard_popup_window 只是我想要填充到弹出窗口中的布局。 - Bohsen
@Jack,有一个PopupWindowCompat类可以实现这个功能。 - Gavin Wright

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接