Android 状态栏和导航栏的控制
2024-02-19 本文已影响0人
yunhen
如果出现问题,可以尝试使用 view.post() ,在里面调用
/**
* 显示虚拟按键和状态栏,取消全屏
* 虚拟按键(三金刚键)和状态栏 一起,称呼为 SystemBar
*
* 里面用到了 {WindowCompat.setDecorFitsSystemWindows(),WindowCompat.getInsetsController()
* ,WindowInsetsControllerCompat.show(WindowInsetsCompat.Type.systemBars())}
* @param activity
*/
public static void showSystemBars(Activity activity) {
//decorFitsSysWin: false标识开启沉浸式,true标识不开启沉浸式
WindowCompat.setDecorFitsSystemWindows(activity.getWindow(),true);
WindowInsetsControllerCompat wicc = WindowCompat.getInsetsController(activity.getWindow()
,activity.getWindow().getDecorView());
wicc.show(WindowInsetsCompat.Type.systemBars());
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
// View v = activity.getWindow().getDecorView();
// WindowInsetsController controller = v.getWindowInsetsController();
// controller.show(WindowInsets.Type.systemBars());
// } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT < Build.VERSION_CODES.R ) {
// //for new api versions.
// View decorView = activity.getWindow().getDecorView();
// int uiOptions = View.SYSTEM_UI_FLAG_VISIBLE;
// decorView.setSystemUiVisibility(uiOptions);
// } else if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { // lower api
// View v = activity.getWindow().getDecorView();
// v.setSystemUiVisibility(View.VISIBLE);
// }
}
/**
* 隐藏虚拟按键(三金刚键)和状态栏,并且全屏 ,虚拟按键和状态栏 一起,称呼为 SystemBar.
* 里面用到了 {WindowCompat.setDecorFitsSystemWindows(),WindowCompat.getInsetsController()
* ,WindowInsetsControllerCompat.hide(WindowInsetsCompat.Type.systemBars())}
* android:fitsSystemWindows=“true”,这个属性表示系统UI(状态栏、导航栏)可见的时候,
* 会给我们的布局加上padding(paddingTop、paddingBottom)属性
*/
public static void hideSystemBars(Activity activity) {
//decorFitsSysWin: false标识开启沉浸式,true标识不开启沉浸式
WindowCompat.setDecorFitsSystemWindows(activity.getWindow(),false);
WindowInsetsControllerCompat wicc = WindowCompat.getInsetsController(activity.getWindow()
,activity.getWindow().getDecorView());
wicc.hide(WindowInsetsCompat.Type.systemBars());
// ViewCompat
//隐藏虚拟按键,并且全屏
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R){
// View v = activity.getWindow().getDecorView();
// WindowInsetsController controller = v.getWindowInsetsController();
// controller.hide(WindowInsets.Type.systemBars());
//
// } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT
// && Build.VERSION.SDK_INT < Build.VERSION_CODES.R ) {
// //for new api versions.
// View decorView = activity.getWindow().getDecorView();
// int uiOptions =
// View.SYSTEM_UI_FLAG_FULLSCREEN
// | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
// | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
// | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
//
// // 它被称作“粘性”的沉浸模式,这个模式会在状态栏和导航栏显示一段时间后,
// // 自动隐藏(你可以点击一下屏幕,立即隐藏)。同时需要重点说明的是,这种模式下,
// // 状态栏和导航栏出现的时候是“半透明”状态,
// | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
// ;
// decorView.setSystemUiVisibility(uiOptions);
// }else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB
// && Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { // lower api
// View v = activity.getWindow().getDecorView();
// v.setSystemUiVisibility(View.GONE);
// }
}