Android弹出窗口未显示在正确的位置

时间:2017-12-05 04:43:23

标签: java android popupwindow

您好我有FlowLayout来显示textViews的列表。当我点击textview时,我必须在PopWindow下面显示textview。但是它没有显示在正确的位置。

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);
    }

OnClick方法:

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

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,所以这就是我在 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])