我有一个按钮:" btnOpenPopup" (在滚动视图中),当我点击按钮时,会出现一个弹出窗口,我可以在屏幕上移动这个弹出窗口
这是我的代码:
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时,如何防止弹出窗口移动? 提前谢谢!
答案 0 :(得分:0)
我认为您需要更改.showAtLocation(..)
参数。
更改
popupWindow.showAsDropDown(btnOpenPopup, 50, -30);
到
popupWindow.showAsDropDown(rootViewId, 50, -30);// RootView id is your main layout id.
我认为您需要添加 ScrollViewId 。
我希望它会帮助你。