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

Android 12中如何在SplashScreen API使用动态系统颜色?

问题原因

Android 12的启动页(SplashScreen)由系统进程在应用启动前绘制,此时应用进程未完全初始化,无法加载解析Material You的动态系统颜色(即@android:color/system_*系列资源)。而静态颜色资源(如@android:color/black或十六进制色值)可被系统进程直接读取,因此能正常生效。

解决方案

方案1:使用静态色值匹配动态颜色效果

直接提取system_neutral1_900对应的十六进制色值(可通过Android Studio颜色选择器或官方颜色对照表获取),替换主题中的动态颜色引用:

<style name="Theme.App.SplashScreen" parent="Theme.SplashScreen">
    <!-- 替换为system_neutral1_900对应的十六进制值,例如深色模式下的#1D1D1F -->
    <item name="windowSplashScreenBackground">#1D1D1F</item>
</style>

这种方式能让系统直接识别颜色,启动页加载时即可显示对应色调。

方案2:应用启动后过渡到动态颜色

如果需要严格匹配系统动态颜色(随系统主题切换而变化),可先设置静态 fallback 色值,再在应用启动后切换为动态颜色并添加过渡动画:

  1. 先在主题中设置静态背景:
<style name="Theme.App.SplashScreen" parent="Theme.SplashScreen">
    <!-- 设置接近动态颜色的静态fallback值 -->
    <item name="windowSplashScreenBackground">#1D1D1F</item>
</style>
  1. 在主Activity中处理启动页过渡:
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val splashScreen = installSplashScreen()
        
        // 将窗口背景设置为动态系统颜色
        window.decorView.setBackgroundColor(getColor(android.R.color.system_neutral1_900))
        
        // 添加退出动画,让过渡更自然
        splashScreen.setOnExitAnimationListener { view ->
            val slideOut = ObjectAnimator.ofFloat(
                view,
                View.TRANSLATION_Y,
                0f,
                -view.height.toFloat()
            )
            slideOut.duration = 500L
            slideOut.interpolator = AccelerateInterpolator()
            slideOut.doOnEnd { view.remove() }
            slideOut.start()
        }
        
        setContentView(R.layout.activity_main)
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.24 18:24:26