按照 Android 文档(还有 this 和 this)实现 Splash Screen API 后,启动活动(在启动屏幕之后)不会像应用程序的其余部分那样具有动态颜色主题。初始屏幕 <style>
具有
<item name="postSplashScreenTheme">@style/Theme.App</item>
属性,该属性应该在清除初始屏幕后将基本应用程序主题应用于启动活动,但事实并非如此。其他所有内容都已按照文档规定的方式实现(活动类、清单文件、活动类和主题.xml 文件)。我已经尝试了所有可能的组合,唯一“有效”的组合是当
不将初始活动主题(或整个活动)设置为清单中的启动屏幕时(即android:theme="@style/Theme.App.Starting"
)。但为什么文档会坚持你这样做呢?我该怎么做才能使其按预期工作?编辑:根据要求,相关代码
启动主题:
<!-- Splash Screen Theme. -->
<style name="Theme.App.Starting" parent="Theme.SplashScreen">
<item name="windowSplashScreenBackground">?android:attr/colorBackground</item>
<item name="windowSplashScreenAnimatedIcon">@mipmap/ic_launcher_round</item>
<item name="windowSplashScreenAnimationDuration">200</item>
<item name="postSplashScreenTheme">@style/Theme.App</item>
<!-- Status bar and Nav bar configs -->
<item name="android:statusBarColor" tools:targetApi="l">?android:attr/colorBackground</item>
<item name="android:navigationBarColor">?android:attr/colorBackground</item>
<item name="android:windowLightStatusBar">true</item>
</style>
清单(仅相关属性):
<application
android:name=".application.App"
android:theme="@style/Theme.App"
tools:targetApi="31">
<activity
android:name=".ui.MainActivity"
android:theme="@style/Theme.App.Starting">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
主要活动:
override fun onCreate(savedInstanceState: Bundle?) {
setupSplashScreen()
super.onCreate(savedInstanceState)
/*...*/
}
应用类别:
class App : Application() {
override fun onCreate() {
super.onCreate()
// Apply dynamic color
DynamicColors.applyToActivitiesIfAvailable(this)
}
}
2023 年 7 月 5 日编辑:(可能)最终更新经过大量的测试和研究,我得出的结论是,这是一个错误,并且不能/不会很快修复。我也指出了原因。
该错误
已经在一年半前被报告过,发生这种情况的原因是在库调用postSplashScreenTheme
之后,它再次设置了应用程序的基本主题,从而摆脱了Material You(动态)颜色。另一个项目遇到了同样的问题,这使得他们无法为他们实施Material You。有人建议在每个 Activity 的
DynamicColors.applyToActivitiesIfAvailable(this)
之后设置
installSplashScreen()
,但是状态栏保留了基本主题颜色而不是动态颜色,所以没有用。在我的实验中,我发现了另一种解决方法:not 在清单中设置任何启动主题 (
android:theme="@style/Theme.App.Starting"
) 似乎适用于 api 级别 31+,但会阻止启动屏幕图标在以前的级别中显示,因此它也是无用的.截至本次更新,Splash Screen API 和 Material You(动态颜色)相互冲突,因此是互斥的。我不会使用动态颜色,因为我需要在应用程序启动时从闪屏 api 加载数据的功能。
setTheme(R.style.Theme_Assignment1);