@capacitor/splash-screen
Splash Screen API 提供了显示或隐藏启动画面的方法。
安装
npm install @capacitor/splash-screen@latest-6
npx cap sync
Android 12 启动画面 API
这仅影响启动时的初始启动画面,在使用编程方式的 show() 方法时不会生效。
Capacitor 4 使用 Android 12 启动画面 API 以及 androidx.core:core-splashscreen 兼容库,使其在 Android 11 及更低版本上也能工作。
可以通过修改 android/app/src/main/res/values/styles.xml 文件中的 AppTheme.NoActionBarLaunch 的父主题,从 Theme.SplashScreen 改为 AppTheme.NoActionBar,来禁用这个兼容库。在 Android 12+ 系统上,Android 12 启动画面 API 无法禁用,因为它是 Android 操作系统的一部分。
<style name="AppTheme.NoActionBarLaunch" parent="AppTheme.NoActionBar">
<item name="android:background">@drawable/splash</item>
</style>
注意:在 Android 12 和 Android 12L 设备上,当从第三方启动器(如 Nova Launcher、MIUI、Realme Launcher、OPPO Launcher 等)、设置应用中的应用信息页面或 Android Studio 等 IDE 中启动应用时,启动画面可能不会显示。 Google Issue Tracker Google Issue Tracker Google 已在 Android 13 中修复了这些问题,但不会将修复方案向后移植到 Android 12 和 Android 12L。与启动器相关的问题可能会通过启动器更新得到修复。如果您在 Android 13 上仍然发现与启动画面相关的问题,请向 Google 报告。
示例
import { SplashScreen } from '@capacitor/splash-screen';
// 隐藏启动画面(应在应用启动时执行)
await SplashScreen.hide();
// 无限期显示启动画面:
await SplashScreen.show({
autoHide: false,
});
// 显示启动画面两秒后自动隐藏:
await SplashScreen.show({
showDuration: 2000,
autoHide: true,
});