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

Jetpack Compose应用启动时如何避免白色背景?

Android 12+ 启动背景颜色不生效的解决办法

Android 12 引入了SplashScreen API,原有的android:windowBackground配置会被系统新的启动屏逻辑覆盖,导致旧配置在Android 12及以上版本失效。以下是针对性的解决步骤:

  • 适配主题配置
    在values-v31/themes.xml(仅针对Android 12+)或全局themes.xml中,添加SplashScreen相关属性:

    <style name="Theme.AlluringScreenshot" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
        <item name="android:statusBarColor">#030318</item>
        <item name="android:windowBackground">#030318</item>
        <!-- Android 12+ 启动屏背景色 -->
        <item name="android:windowSplashScreenBackground">#030318</item>
        <!-- 若不需要启动屏图标,设置为透明隐藏 -->
        <item name="android:windowSplashScreenAnimatedIcon">@android:color/transparent</item>
        <!-- 取消启动屏动画,避免过渡闪白 -->
        <item name="android:windowSplashScreenAnimationDuration">0</item>
    </style>
    
  • 配合Compose根布局设置
    在Activity的setContent中,给根布局设置与启动背景一致的颜色,避免启动屏切换到Compose内容时出现闪白:

    setContent {
        AlluringScreenshotTheme {
            Box(modifier = Modifier.fillMaxSize().background(Color(0xFF030318))) {
                // 应用内容布局
            }
        }
    }
    
  • 可选:控制启动屏显示时机
    若需要等待Compose初始化完成再隐藏启动屏,可在Activity的onCreate中添加:

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
        val splashScreen = installSplashScreen()
        // 根据实际初始化逻辑调整条件,这里示例直接隐藏
        splashScreen.setKeepOnScreenCondition { false }
    }
    

内容的提问来源于stack exchange,提问作者Thái Quốc Toàn

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 05:35:16