Android 顶层浮动窗口
Manifest 权限要求
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
代码实现
WindowManager wm = (WindowManager) Context.getApplicationContext().getSystemService(Application.WINDOW_SERVICE); // 从上下文得到窗口管理对象 WindowManager.LayoutParams wml = new WindowManager.LayoutParams(); // 窗口参数 wml.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT | WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY; wml.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL; wml.width = 200; // 窗口宽度 wml.height = 200; // 窗口高度 wml.format = PixelFormat.RGBA_8888; // 保证透明 wml.gravity = Gravity.NO_GRAVITY; // 屏幕对齐方式--NO_GRAVITY(效果是从屏幕中心开始的) View floatWin; // 浮动的窗口View wm.addView(floatWin, wml); // 添加到顶层窗口,并显示