android之popupwindow点击空白消失

查了查 大部分有两个方法

方法1:

protected void showPopupWindow() {
		//Context mContext = HahItemActivity.this;
		LayoutInflater mLayoutInflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
		//保存转发等视图
		View xuanz_popunwindwow = mLayoutInflater.inflate(
				R.layout.popwindow_image, null);
		//转化为popunwindow
		mPopupWindow= new PopupWindow(xuanz_popunwindwow,
				LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
		
		mPopupWindow.setBackgroundDrawable(new BitmapDrawable());   
		mPopupWindow.setFocusable(true);
		mPopupWindow.setOutsideTouchable(true);
		mPopupWindow.update();
		//设置显示
		mPopupWindow.showAtLocation(findViewById(R.id.hahaimage_pop_layout), Gravity.CENTER, 0,0);

//设置显示mPopupWindow.showAtLocation(findViewById(R.id.hahaimage_pop_layout), Gravity.CENTER, 0,0);
mPopupWindow.setBackgroundDrawable(new BitmapDrawable());   //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
		mPopupWindow.setFocusable(true);		//!获取焦点!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1
		mPopupWindow.setOutsideTouchable(true);	mPopupWindow.update();				//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1
上面这三个语句就可以控制啦点击别的地方消失啦,要写在ShowAtLocation之前 .()设置popupWindow.setFocusable(true); 这样才能让popupWindow里面的布局控件获得点击的事件否则就被它的父亲view给拦截了。


方法2 :(这种方法可以处理popupwindows dimiss的时候一些其他的操作,比如让其他控件的隐藏,消失等):
代码如下popupWindow.setFocusable(false);//focusable要为false(不设置默认的就是False);
//这是Activity 的OnTouchEvent。OnTouchEvent代表的是Activity 获得事件(即为PopupWindow之外)
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
if (popupWindow != null && popupWindow.isShowing()) {
popupWindow.dismiss();
popupWindow = null;
}
return super.onTouchEvent(event);
}


注:亲测1可行

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值