popupwindow 增加半透明遮罩

 

//在show的时候加入addmask

 

private void addMask(IBinder token) {

WindowManager.LayoutParams wl = new WindowManager.LayoutParams();

wl.width = WindowManager.LayoutParams.MATCH_PARENT;

wl.height = WindowManager.LayoutParams.MATCH_PARENT;

wl.format = PixelFormat.TRANSLUCENT;//不设置这个弹出框的透明遮罩显示为黑色

wl.type = WindowManager.LayoutParams.TYPE_APPLICATION_PANEL;//该Type描述的是形成的窗口的层级关系

wl.token = token;//获取当前Activity中的View中的token,来依附Activity

maskView = new View(context);

maskView.setBackgroundColor(0x7f000000);

maskView.setFitsSystemWindows(false);

maskView.setOnKeyListener(new View.OnKeyListener() {

@Override

public boolean onKey(View v, int keyCode, KeyEvent event) {

if (keyCode == KeyEvent.KEYCODE_BACK) {

removeMask();

return true;

}

return false;

}

});

/**

* 通过WindowManager的addView方法创建View,产生出来的View根据WindowManager.LayoutParams属性不同,效果也就不同了。

* 比如创建系统顶级窗口,实现悬浮窗口效果!

*/

windowManager.addView(maskView, wl);

}

 

public void removeMask() {

if (null != maskView) {

windowManager.removeViewImmediate(maskView);

maskView = null;

}

}

 

 

 

//记得监听popupwindow消失时还原activity,

window.setOnDismissListener(new PopupWindow.OnDismissListener() {

@Override

public void onDismiss() {

removeMask();

}

});

你可能感兴趣的:(Android,技术干货)