Cordova Android端Splash Screen(启动页)无法正常显示求助
Cordova启动页(Splash Screen)无法正常显示解决指南
问题描述
使用Cordova开发应用,其他功能正常,但启动页始终显示默认Cordova图标,无法加载自定义启动页资源。
我的配置与环境
config.xml代码
<?xml version='1.0' encoding='utf-8'?> <widget id="com.example.hello" version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0"> <name>HelloWorld</name> <description>Sample Apache Cordova App</description> <author email="dev@cordova.apache.org" href="https://cordova.apache.org"> Apache Cordova Team </author> <content src="index.html" /> <allow-intent href="http://*/*" /> <allow-intent href="https://*/*" /> <platform name="android"> <preference name="SplashScreen" value="screen" /> <preference name="SplashScreenDelay" value="3000" /> <preference name="SplashMaintainAspectRatio" value="true" /> <icon density="ldpi" src="res/icon/android/icon-36-ldpi.png" /> <!-- Use the AndroidWindowSplashScreen preferences for splash screen configurations --> <preference name="AndroidWindowSplashScreen" value="screen" /> <preference name="AndroidWindowSplashScreenAnimationDuration" value="2000" /> <preference name="AndroidWindowSplashScreenContent" value="res/screen/android/land-ldpi.png" /> <!-- Add more splash screen configurations for other densities --> </platform> </widget>
文件结构
projectRoot ├─platforms ├─plugins ├─www ├─res │ ├─screen │ │ ├─android │ │ │ └─land-ldpi.png │ │ └─ios
问题现象
启动页无法正常显示,始终展示默认的Cordova图标:
2023年有效解决步骤
1. 升级依赖组件
- 确保安装最新版
cordova-plugin-splashscreen和适配Android 12+的cordova-android(最低11.0.0版本,Android 12引入了新的SplashScreen API) - 执行命令:
cordova plugin add cordova-plugin-splashscreen@latest cordova platform update android@latest
2. 修正config.xml配置
你的配置混用了旧版插件和Android 12+新启动页规则,按以下方式调整:
<platform name="android"> <!-- 旧版Android兼容配置 --> <preference name="SplashScreen" value="screen" /> <preference name="SplashScreenDelay" value="3000" /> <preference name="SplashMaintainAspectRatio" value="true" /> <!-- Android 12+ 新启动页配置 --> <preference name="AndroidWindowSplashScreen" value="true" /> <preference name="AndroidWindowSplashScreenAnimationDuration" value="2000" /> <!-- 无需手动指定单张图片,使用工具自动生成全密度资源 --> </platform>
注意:
AndroidWindowSplashScreen值应为true而非资源名称,Android 12+会自动识别生成的drawable资源。
3. 补全启动页资源
目前仅提供了land-ldpi.png,缺少其他密度(mdpi/hdpi/xhdpi/xxhdpi/xxxhdpi)的横竖屏图片,推荐用cordova-res自动生成:
- 安装工具:
npm install -g cordova-res - 在项目根目录创建
resources文件夹,放入一张高分辨率启动页原图(如splash.png,尺寸≥2732×2732) - 生成全平台资源:
cordova-res android --skip-config --copy
若手动添加,需在res/screen/android下补充以下文件:
- 竖屏:
port-ldpi.png、port-mdpi.png、port-hdpi.png、port-xhdpi.png、port-xxhdpi.png、port-xxxhdpi.png - 横屏:
land-mdpi.png、land-hdpi.png、land-xhdpi.png、land-xxhdpi.png、land-xxxhdpi.png
4. 清理重建项目
- 清理旧构建缓存:
cordova clean android - 重新构建:
cordova build android - 卸载设备上的旧应用后重新安装:
cordova run android
5. 排查常见问题
- 检查
res/screen/android下的图片路径与config.xml引用是否一致 - 用
cordova plugin list确认cordova-plugin-splashscreen已安装 - 查看
platforms/android/app/src/main/AndroidManifest.xml,确认启动页相关配置已自动生成
内容的提问来源于stack exchange,提问作者Legendchyke
相关产品推荐
相关产品推荐

