statusbar状态栏设置

443 阅读1分钟

设置沉浸式

// false:沉浸式; true:正常模式
WindowCompat.setDecorFitsSystemWindows(getWindow(), false);

设置statusbar、navgator颜色

getWindow().setStatusBarColor( Color.BLUE); // 状态颜色
getWindow().setNavigationBarColor(Color.BLUE);// navigatebar颜色
getWindow().setNavigationBarDividerColor( Color.BLUE); // navigatebar分割线颜色

状态栏显示隐藏

WindowInsetsControllerCompat insetsController = ViewCompat.getWindowInsetsController(getWindow().getDecorView());
insetsController.show(WindowInsetsCompat.Type.statusBars()); // 显示状态栏
insetsController.hide(WindowInsetsCompat.Type.statusBars()); // 隐藏状态栏

隐藏状态栏会退出沉浸式activity内容区域会减除statusbar。

导航栏显示隐藏

WindowInsetsControllerCompat insetsController = ViewCompat.getWindowInsetsController(getWindow().getDecorView());
insetsController.hide(WindowInsetsCompat.Type.navigationBars()); // 隐藏导航栏
insetsController.show(WindowInsetsCompat.Type.navigationBars()); // 显示导航栏

内容区展示同状态栏设置。

系统区域显示隐藏

WindowInsetsControllerCompat insetsController = ViewCompat.getWindowInsetsController(getWindow().getDecorView());
insetsController.hide(WindowInsetsCompat.Type.systemBars()); // 隐藏状态栏、导航栏、标题栏
insetsController.show(WindowInsetsCompat.Type.systemBars()); // 显示状态栏、导航栏、标题栏

键盘显示隐藏

WindowInsetsControllerCompat insetsController = ViewCompat.getWindowInsetsController(getWindow().getDecorView());
insetsController.show(WindowInsetsCompat.Type.ime()); // 显示键盘
insetsController.hide(WindowInsetsCompat.Type.ime()); // 隐藏键盘

状态栏文本颜色

WindowInsetsControllerCompat insetsController = ViewCompat.getWindowInsetsController(getWindow().getDecorView());
insetsController.setAppearanceLightStatusBars(false); // 状态栏文本 false:白色 ; true:黑色