Android 隐藏系统栏(API 21 - API 32)
val decorView: View = window.decorView //侵入系统栏 WindowCompat.setDecorFitsSystemWindows(window,!hide) /**API 28 以上 状态栏位置上被黑条占领 问题解决 **/ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { val lp = window.attributes lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES window.attributes = lp } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { val controller: WindowInsetsController? = decorView.windowInsetsController //从边缘滑动以显示隐藏栏。 无论导航栏的可见性如何,手势导航都有效。 //API level 31 // controller?.systemBarsBehavior = WindowInsetsController.BEHAVIOR_DEFAULT //“粘性沉浸模式”。 从边缘滑动以暂时显示隐藏栏。 controller?.systemBarsBehavior = WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE //WindowInsets.Type.navigationBars() (底部)导航栏 //WindowInsets.Type.statusBars() (顶部)状态栏 //WindowInsets.Type.systemBars() (底部和顶部)系统栏 if(!hide) controller?.show/*显示*/(WindowInsets.Type.systemBars()) else controller?.hide/*隐藏*/(WindowInsets.Type.systemBars()) } else { window.decorView.systemUiVisibility = ( View.SYSTEM_UI_FLAG_IMMERSIVE // // 将内容设置为显示在系统栏下方,以便在系统栏隐藏和显示时内容不会调整大小。 or View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN //隐藏导航栏和状态栏 or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_FULLSCREEN ) }
或者
<style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar"> <!-- 隐藏状态栏 --> <item name="android:windowFullscreen">true</item> <!-- 隐藏标题栏 --> <item name="android:windowNoTitle">true</item> </style>
动态窗口
WindowCompat.setDecorFitsSystemWindows(window, false) ViewCompat.setOnApplyWindowInsetsListener( window.decorView ) { v, insets -> val imeHeight = insets.getInsets(WindowInsetsCompat.Type.ime()).bottom v.setPadding( 0, 0, 0, imeHeight + insets.getInsets(WindowInsetsCompat.Type.navigationBars()).bottom ) insets } window.statusBarColor = Color.TRANSPARENT