【问题标题】:PopupWindow goes out screen's bottom boundaryPopupWindow 走出屏幕的底部边界
【发布时间】:2018-10-11 07:11:53
【问题描述】:

这是一个简化的代码,用于在长按 GridView 项目时显示弹出窗口。

当最右边的项目调整为在屏幕内时弹出窗口。

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

如何解决这个问题?

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.
 *

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

【问题讨论】:

  • 能否提供截图以便更好理解。
  • 我也有同样的问题。这里有一个旧帖子stackoverflow.com/questions/13115401/…,但没有给出可行的解决方案。
  • 我也尝试过在 API 22 和 25 上使用 PopupWindowCompat。没有区别。问题依然存在。
  • 如果你明确设置高度和宽度,它可以工作:PopupWindow(popupView, 100, 100).apply { setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT)) isTouchable = true isFocusable = true showAsDropDown(fragment_person_details__description) }

标签: android android-popupwindow


【解决方案1】:

事实证明,我必须设置下拉菜单高度以避免这个问题

List<DropDownListItem> items;

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

【讨论】:

    【解决方案2】:

    找到了解决办法。您必须测量视图并在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,但这是 Android 代码吗?几乎所有的代码都没有定义,包括 R.layout.standard_popup_window。
    • 另外 setOverlapAnchor(boolean) 需要 API 23,我的目标是 API 16+
    • 这是使用 android-kotlin 扩展的 Android kotlin 代码。只需放下 setOverlapAnchor,应该没关系。应该没有它。
    • R.layout.standard_popup_window 只是我想在弹出窗口中添加的布局
    • @Jack 有一个 PopupWindowCompat 类。
    猜你喜欢
    • 2023-03-27
    • 1970-01-01
    • 2018-07-30
    • 2021-07-17
    • 2012-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-12
    相关资源
    最近更新 更多