View view=LayoutInflater.from(this).inflate(R.layout.clean_pop, null);
// popupWindow = new PopupWindow(view, 300, 200);
popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, false);
// 需要设置一下此参数,点击外边可消失
popupWindow.setBackgroundDrawable(new BitmapDrawable());
//设置点击窗口外边窗口消失
popupWindow.setOutsideTouchable(true);
// 设置此参数获得焦点,否则无法点击
popupWindow.setFocusable(true);
if(popupWindow.isShowing()) {
// 隐藏窗口,如果设置了点击窗口外小时即不需要此方式隐藏
popupWindow.dismiss();
} else {
// 显示窗口
// popupWindow.isShowing();
popupWindow.showAtLocation(v, Gravity.CENTER, 0, 0);
darkenBackground(0.2f);
}
popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
darkenBackground(1f);
}
});
//____________________________
/**
* 改变背景颜色
*/
private void darkenBackground(Float bgcolor){
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.alpha = bgcolor;
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
getWindow().setAttributes(lp);
}