本文记录使用 Popwindow 时遇到的问题和解决方案,以及 popwindow 所有相关的技术点。
1. android PopupWindow 点击外面消失
有个需求是点击外部,popwindow 会消失,通过设置相关属性,发现一直没有效果,通过查阅资料,发现主要是以下原因造成的:
showAtLocation调用之前,加上以下两句,
popupWindow.setOutsideTouchable(true);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
发现然并卵,百思不得其解,经过反复思考,发现这一段代码写得有问题:
popupWindow = new PopupWindow(LayoutInflater.from(MainActivity.this)
.inflate(R.layout.pop_list, null), ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
改成如下:
popupWindow = new PopupWindow(LayoutInflater.from(MainActivity.this)
.inflate(R.layout.pop_list, null), ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
原因很简单,因为前者这样写得话,popupWindow 把整个 window 都遮挡了,所以 popupWindow.setOutsideTouchable(true) 并没有起到作用,所以失效。
参考链接:https://www.cnblogs.com/sharkli/p/5518883.html