弹出窗口Android



我有一个按钮:" btnopenpopup"(在scrollview中),当我单击按钮时,将出现一个弹出窗口,我可以在屏幕上移动此弹出窗口

>

这是我的代码:

final ImageButton btnOpenPopup = (ImageButton) findViewById(R.id.buttonUndoRedo);
    btnOpenPopup.setOnClickListener(new ImageButton.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            if (popupWindowEnabled == true) {
                popupWindowEnabled = false;
                LayoutInflater layoutInflater =
                        (LayoutInflater) getBaseContext()
                                .getSystemService(LAYOUT_INFLATER_SERVICE);
                final View popupView = layoutInflater.inflate(R.layout.popup_window, null);
                final PopupWindow popupWindow = new PopupWindow(
                        popupView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                // Font
                TextView txt = (TextView) popupView.findViewById(R.id.textViewPopupWindow);
                Typeface myNewFace = Typeface.createFromAsset(getAssets(), "fonts/futura.ttf");
                txt.setTypeface(myNewFace);
                Button btnDismiss = (Button) popupView.findViewById(R.id.dismiss);
                btnDismiss.setOnClickListener(new Button.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        popupWindow.dismiss();
                        popupWindowEnabled = true;
                    }
                });
                popupWindow.showAsDropDown(btnOpenPopup, 50, -30);
                popupView.setOnTouchListener(new View.OnTouchListener() {
                    int orgX, orgY;
                    int offsetX, offsetY;
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        //Toast.makeText(getApplicationContext(), "dEMO", Toast.LENGTH_SHORT).show();
                        switch (event.getAction()) {
                            case MotionEvent.ACTION_DOWN:
                                orgX = (int) event.getX();
                                orgY = (int) event.getY();
                                break;
                            case MotionEvent.ACTION_MOVE:
                                offsetX = (int) event.getRawX() - orgX;
                                offsetY = (int) event.getRawY() - orgY;
                                popupWindow.update(offsetX, offsetY, -1, -1, true);
                                break;
                        }
                        return true;
                    }
                });
            }
        }
    });

问题是当我在scrollview中移动btnopenpopup时,弹出窗口将离开其位置并遵循btnopenpopup

当我们移动btnopenpopup时,如何防止弹出窗口移动?预先感谢!

我认为您需要更改.showAtLocation(..)参数。

更改

popupWindow.showAsDropDown(btnOpenPopup, 50, -30);

to

popupWindow.showAsDropDown(rootViewId, 50, -30);// RootView id is your main layout id.

我认为您需要添加 scrollViewId

我希望它对您有所帮助。

最新更新