1, Activity 铺满android 界面
在oncreate里设置
if (Build.VERSION.SDK_INT >= 21) {
View decorView = getWindow().getDecorView();
decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
getWindow().setStatusBarColor(Color.TRANSPARENT);
}
其原理就是给最外层的View设置系统样式偏好。SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN 就是铺满Activity,View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR 代表黑色status字体
2,获取status bar的宽高
此方式获取到的不过是不包括标题栏宽高:
A
WindowManager windowManager = (WindowManager) application.getSystemService(Context.WINDOW_SERVICE);
wrapper.MAX_HEIGHT = windowManager.getDefaultDisplay().getHeight();
wrapper.MAX_WIDTH = windowManager.getDefaultDisplay().getWidth();
然后此方式反射获得才是最终的宽高(重点是高度)
B
Field field = Display.class.getDeclaredField("mDisplayInfo");
field.setAccessible(true);
Object displayInfo = field.get(windowManager.getDefaultDisplay());
Field fieldWidth = displayInfo.getClass().getDeclaredField("logicalWidth");
Field fieldHeight = displayInfo.getClass().getDeclaredField("logicalHeight");
wrapper.MAX_LOCAL_WIDTH = fieldWidth.getInt(displayInfo);
wrapper.MAX_LOCAL_HEIGHT = fieldHeight.getInt(displayInfo);
所以最终状态栏宽高是:
status bar height = B.height - A.height