可能重复:
How to dismiss the dialog with click on outside of the dialog?
如何在弹出窗口外单击以将其取消?
这是我的代码:
cell.setOnClickListener(new OnClickListener(){
/*This code is in a separate class so I needed to use ctx as context
*and the string "layout_inflater" because it was not recognizing
*LAYOUT_INFLATER_SERVICE*/
@Override
public void onClick(View arg0) {LayoutInflater layoutInflater =
(LayoutInflater)ctx.getSystemService("layout_inflater");
View popupView = layoutInflater.inflate(R.layout.popup_window, null);
final PopupWindow popupWindow = new PopupWindow(popupView,
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
popupWindow.showAtLocation(newParentLayout, Gravity.CENTER, 0, 0);
}
我也试过添加所有这些,但没有结果。
popupWindow.setTouchable(true);
popupWindow.setFocusable(true);
popupWindow.setOutsideTouchable(true);
Drawable bg = ctx.getResources().getDrawable(R.drawable.popup_bg);
popupWindow.setBackgroundDrawable(bg);
我没主意了。有什么帮助吗?
编辑添加:主布局是一个viewpager/pageradapter,如果这会影响什么?
最佳答案
请将setOutsideTouchable(true)与背景一起设置。这对我很有用。我知道将背景设置为空会杀死OntouchListener。
关于android - 在弹出窗口之外单击以将其关闭,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12554855/