我正在尝试创建一种具有popupMenuItem和自定义布局的popupWindow。
我有一个按钮,当我点击它时,它会显示popupWindow。当我再次单击此按钮或单击popupWindow外部时,我希望触发和事件关闭此popupWindow。
但是现在它不起作用了,我的setTouchInterceptor还没有触发,你有办法解决这个问题吗?
每次打开这个popupWindow,我都无法访问所有其他UI元素,即使使用setOutsideTouchable(true)。
这里我的代码:
另外,我也尝试过这样做,但没有奏效:
popupMenuImageView = (ImageView) getView().findViewById(R.id.popup_menu_imageView);
createMenuItem();
popupMenuImageView.setOnClickListener(eventOpenCloseMenuItem);
View.OnClickListener eventOpenCloseMenuItem = new View.OnClickListener(){
@Override
public void onClick(View v) {
if (isShowing) {
popupWindow.showAsDropDown(popupMenuImageView);
isShowing = false;
}else{
popupWindow.dismiss();
}
}
};私有空createMenuItem(){ 如果( getView().findViewById(R.id.custom_popup_menu_id);== null) { int宽度= 375;int高度= 240;tableLayout TableLayout = (TableLayout) == layoutInflater LayoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);View layout = layoutInflater.inflate(R.layout.custom_popup_menu,tableLayout);popupWindow =新PopupWindow(布局、宽度、高度、真);popupWindow.setOutsideTouchable(假);popupWindow.setFocusable(真);}
发布于 2015-01-08 15:01:58
如果您只想通过单击弹出窗口外部关闭弹出窗口,则不需要设置拦截器。只需将背景可绘图设置为空,例如
popupWindow.setBackgroundDrawable (new BitmapDrawable());
popupWindow.setOutsideTouchable(true);并删除:
popupWindow.setTouchInterceptor(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_OUTSIDE){
popupWindow.dismiss();
return true;
}
return false;
}
});请查查我对类似问题的回答,PopupWindow - Dismiss when clicked outside
发布于 2015-01-08 15:01:33
我相信您需要在显示弹出窗口之前先使用setBackgroundDrawable()才能处理触摸事件:
popupWindow.setBackgroundDrawable(new ColorDrawable());https://stackoverflow.com/questions/27842673
复制相似问题