【问题标题】:Android Popup Window is not showing in correct locationAndroid 弹出窗口未显示在正确的位置
【发布时间】:2017-12-05 04:43:23
【问题描述】:

嗨,我有一个FlowLayout 来显示textViews 的列表。当我单击textview 时,我必须在textview 下方显示PopWindow。但它没有显示在正确的位置。

public void showPopUpWindow(View view, int position) {
        LinearLayout linearLayout = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.layout_attributes_popup, null);
        FlowLayout flowLayout = linearLayout.findViewById(R.id.flow_layout);
        BubbleLayout bubbleLayout = linearLayout.findViewById(R.id.bubble_layout);
        prepareAttributes(flowLayout);
        PopupWindow mPopupWindow = BubbleHelper.create(this, linearLayout);
        mPopupWindow.setFocusable(true);
        mPopupWindow.update();
        if (mPopupWindow != null) {
            mPopupWindow.dismiss();
        }

        Rect rc = new Rect();
        view.getWindowVisibleDisplayFrame(rc);
        int[] xy = new int[2];
        view.getLocationInWindow(xy);
        rc.offset(xy[0], xy[1]);

        //int[] location = new int[2];
        // view.getLocationInWindow(location);
        bubbleLayout.setArrowPosition(rc.left);
        //mPopupWindow.showAsDropDown(view, 0, 0);

        mPopupWindow.showAtLocation(view, Gravity.NO_GRAVITY, rc.left, rc.top);
    }

点击方法:

textView.setOnClickListener(view1 -> ((RichElementSelectionActivity) mContext).
                        showPopUpWindow(view1, mFlowLayout.indexOfChild((View) view1.getParent())));

【问题讨论】:

  • 你在哪里调用 showPopUpWindow 方法
  • @Nilu textview 我在 textview setOnClickListener 方法中调用
  • 请与问题分享该代码
  • 请提供minimal reproducible example。共享无法正确查看的屏幕截图也是无用的。提供更高分辨率的屏幕截图。
  • @Nilu 请检查我更新了

标签: java android popupwindow


【解决方案1】:

我遇到了同样的问题,所以这就是我在kotlin 中解决的方法

view 是您想用作弹出窗口锚点的任何视图

        val popupView: View = layoutInflater.inflate(R.layout.popup_view_order_status, null)
        val popupWindow = PopupWindow(popupView,
                FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT)
        popupWindow.isFocusable = true
        popupWindow.setBackgroundDrawable(ColorDrawable())
        val location = IntArray(2)
        view.getLocationOnScreen(location)
        popupWindow.showAtLocation(view, Gravity.NO_GRAVITY, location[0], location[1])

【讨论】:

    猜你喜欢
    • 2014-01-24
    • 1970-01-01
    • 2022-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多