Android动态的显示和隐藏状态栏,亲测有效

    private fun full(enable: Boolean) {
        kotlin.runCatching {
            if (enable) {
                window.setFlags(
                    WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN
                );
                window.decorView.systemUiVisibility =
                    View.SYSTEM_UI_FLAG_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                val lp = window.attributes
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                    lp.layoutInDisplayCutoutMode =
                        WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
                }
                window.attributes = lp;
            } else {
                val attr: WindowManager.LayoutParams = window.attributes
                attr.flags = attr.flags and WindowManager.LayoutParams.FLAG_FULLSCREEN.inv()
                window.attributes = attr
                window.clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS)
            }
        }
    }

参数为true时,隐藏状态栏,参数为false时,显示状态栏

猜你喜欢

转载自blog.csdn.net/m0_37707561/article/details/128388677
今日推荐