android中popupwindow使用技巧
在项目中遇到,点击popupwindow背景activity界面,使得popupwindow消失。
网上查阅了相关资料,最后实现:
?
?
window.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_tcbox));window.setOutsideTouchable(true);
? ? 这两段代码很重要
?
?
? ?popupWindow = getPopWindow();
popupWindow.showAtLocation(findViewById(R.id.ll_root), Gravity.BOTTOM, 0, 0);protected PopupWindow getPopWindow() {View popupWindow_view = getLayoutInflater().inflate( // 获取自定义布局文件dialog.xml的视图R.layout.dg_weibo_detail_more, null, false);PopupWindow window = new PopupWindow(popupWindow_view, ResolutionUtil.getWPx(),ResolutionUtil.getHPx() / 3, true);// 创建PopupWindow实例window.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_tcbox));window.setOutsideTouchable(true);return window;}@Overridepublic boolean onTouchEvent(MotionEvent event) {if (popupWindow != null && popupWindow.isShowing()) {popupWindow.dismiss();popupWindow = null;}return super.onTouchEvent(event);}