亲测可用.
public abstract class BottomPushPopupWindow extends PopupWindow {
protected Context context;
private WindowManager wm;
private View maskView;
@SuppressWarnings("deprecation")
public BottomPushPopupWindow(Context context, T t) {
super(context);
this.context = context;
wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
initType();
setContentView(generateCustomView(t));
setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
setOutsideTouchable(true);
setFocusable(true);
setBackgroundDrawable(context.getResources().getDrawable(android.R.color.transparent));
setAnimationStyle(R.style.Animations_BottomPush);
}
protected abstract View generateCustomView(T t);
@TargetApi(23)
private void initType() {
setWindowLayoutType(WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL);
}
@Override
public void showAtLocation(View parent, int gravity, int x, int y) {
addMaskView(parent.getWindowToken());
super.showAtLocation(parent, gravity, x, y);
}
@Override
public void showAsDropDown(View anchor, int xoff, int yoff) {
addMaskView(anchor.getWindowToken());
super.showAsDropDown(anchor, xoff, yoff);
}
@Override
public void dismiss() {
removeMaskView();
super.dismiss();
}
/**
* 显示在界面的底部
*/
public void show(Activity activity) {
showAtLocation(activity.getWindow().getDecorView(), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
}
private void addMaskView(IBinder token) {
WindowManager.LayoutParams p = new WindowManager.LayoutParams();
p.width = WindowManager.LayoutParams.MATCH_PARENT;
p.height = WindowManager.LayoutParams.MATCH_PARENT;
p.format = PixelFormat.TRANSLUCENT;
p.type = WindowManager.LayoutParams.TYPE_APPLICATION_PANEL;
p.token = token;
p.windowAnimations = android.R.style.Animation_Toast;
maskView = new View(context);
maskView.setBackgroundColor(0x7f000000);
maskView.setFitsSystemWindows(false);
maskView.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
removeMaskView();
return true;
}
return false;
}
});
wm.addView(maskView, p);
}
private void removeMaskView() {
if (maskView != null) {
wm.removeViewImmediate(maskView);
maskView = null;
}
}
}