You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android系统栏(状态栏、导航栏)隐藏后显示时白色闪烁问题求助

System Bar(状态栏、导航栏)隐藏后再次显示时出现白色闪烁问题

问题现象

  • 平板设备表现:
    平板设备上的白色闪烁现象
  • 横屏手机表现:
    横屏手机上的白色闪烁现象

复现代码

核心控制代码如下:

findViewById<Button>(R.id.show).setOnClickListener {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
        window?.insetsController?.let {
            window.setDecorFitsSystemWindows(true)
            window.insetsController?.show(WindowInsets.Type.systemBars())
        }
    } else {
        window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
        window.decorView.systemUiVisibility = 0
    }
}

findViewById<Button>(R.id.hide).setOnClickListener {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
        window?.insetsController?.let {
            window.setDecorFitsSystemWindows(false)
            it.systemBarsBehavior = WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
            it.hide(WindowInsets.Type.systemBars())
        }
    } else {
        window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN)
        window.decorView.systemUiVisibility =
            (View.SYSTEM_UI_FLAG_FULLSCREEN or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY)
    }
}

问题范围

该问题在 API ≥30(Android 11)和 API ≤29(Android 10)版本中均会出现,是否遗漏了必要的配置或处理?

解决方案

1. 统一设置System Bar背景色

白色闪烁多因System Bar显示时默认浅色背景与页面反差过大导致,可提前设置和页面适配的背景色:

// API ≥21适用
window.statusBarColor = ContextCompat.getColor(this, R.color.your_page_background)
window.navigationBarColor = ContextCompat.getColor(this, R.color.your_page_background)

2. 调整API≥30的System Bar显示逻辑

显示System Bar时,先调用show()再设置setDecorFitsSystemWindows(true),避免布局重绘引发闪烁:

findViewById<Button>(R.id.show).setOnClickListener {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
        window?.insetsController?.let { controller ->
            controller.show(WindowInsets.Type.systemBars())
            window.setDecorFitsSystemWindows(true)
        }
    } else {
        // 旧版本调整:先恢复系统UI可见性,再清除全屏flag
        window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_VISIBLE
        window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
    }
}

3. 布局适配优化

给布局根视图添加android:fitsSystemWindows="true",确保布局与System Bar无缝衔接,避免显示时的布局跳动:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">
    <!-- 页面布局内容 -->
</LinearLayout>

4. 可选:关闭过渡动画

若上述方法无效,可尝试关闭System Bar的显示过渡动画:

// API ≥30适用
window.insetsController?.setSystemBarsAppearance(0, WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS)

内容的提问来源于stack exchange,提问作者qarasique

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 21:13:10