通常情况下悬浮窗只会显示在状态下方,在某些应用场景,我们需要将内容填充之至状态栏中。
重点总共两点:
1.将window的高度设置为屏幕高+状态栏高
2.将window的flag设置允许拓展到状态栏
代码附上:
WindowManager wm;
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
wm = (WindowManager) mService.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
lp.height = getScreenHeight(mService) + getStatusBarHeight(mService);
lp.flags =WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
| WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN//将window放置在整个屏幕之内,无视其他的装饰(比如状态栏)
| WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS//允许window扩展值屏幕之外
| WindowManager.LayoutParams.FLAG_FULLSCREEN//当这个window显示的时候,隐藏所有的装饰物(比如状态栏)这个flag允许window使用整个屏幕区域
| WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER//标记在其它窗口的中的存在情况而不断地被调整
;
lp.type = Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ? WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY : WindowManager.LayoutParams.TYPE_PHONE;
if (wm.getDefaultDisplay() != null && wm.getDefaultDisplay().isValid()) {
wm.addView(root, lp);
}
获取状态栏的高度:
public static int getStatusBarHeight(Context context) {
Resources resources = context.getResources();
int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
int height = resources.getDimensionPixelSize(resourceId);
return height;
}
获取屏幕的高度:
public static int getScreenHeight(Context context) {
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
return wm.getDefaultDisplay().getHeight();
}