从下方弹出的PopupWindow,仿iphone效果,增加半透明蒙层。
16lz
2022-07-27
亲测可用.
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);}@Overridepublic void showAtLocation(View parent, int gravity, int x, int y) { addMaskView(parent.getWindowToken()); super.showAtLocation(parent, gravity, x, y);}@Overridepublic void showAsDropDown(View anchor, int xoff, int yoff) { addMaskView(anchor.getWindowToken()); super.showAsDropDown(anchor, xoff, yoff);}@Overridepublic 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; }}
}
更多相关文章
- Android(安卓)Activity界面切换添加动画特效
- Android(安卓)拨号器的简单实现
- android用户界面之按钮(Button)教程实例汇
- 锁屏界面
- Android(安卓)P SystemUI之StatusBar UI布局status_bar.xml解析
- android用户界面-组件Widget-地图视图MapView
- android用户界面-组件Widget-画廊视图Gallery
- Android中判断网络功能是否可用
- Android中使Dialog显示时背景不变暗