抬头仰望星空,是否能发现自己的渺小。

伪斜杠青年

人们总是混淆了欲望和理想

Android状态栏、虚拟按键隐藏/显示扩展方法

最近经常处理这个,所以直接写成了扩展函数,在kotlin中挺好用的。记录下:

/**
* 隐藏状态栏、虚拟按键
* 锁屏后打开屏幕依旧隐藏 需要配和监听setOnSystemUiVisibilityChangeListener
*/
fun Window.makeFullScreenBottomSheet() {
val uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION.or(View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION)
.or(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)
.or(View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY)
decorView.systemUiVisibility = uiOptions
addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
}

/**
* 隐藏状态栏、虚拟按键
* 锁屏后打开屏幕虚拟按键会弹出
*/
fun Window.makeFullScreen() {
val uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION.or(View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION)
.or(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)
.or(View.SYSTEM_UI_FLAG_IMMERSIVE)
decorView.systemUiVisibility = uiOptions
addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
}

/**
* 退出全屏模式
*/
fun Window.exitFullScreen() {
val uiOptions = View.SYSTEM_UI_FLAG_VISIBLE
decorView.systemUiVisibility = uiOptions
clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
}

/**
* 隐藏状态栏
*/
fun Window.hideStatusBar() {
addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
}

/**
* 显示状态栏
*/
fun Window.showStatusBar() {
clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
}

/**
* 只隐藏虚拟按键
*/
fun Window.makeHideNaviBottomSheet() {
val uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION.or(View.SYSTEM_UI_FLAG_IMMERSIVE)
decorView.systemUiVisibility = uiOptions
}

参考文章直接看谷歌的即可:

https://developer.android.com/training/system-ui/immersive


0条评论

发表评论