我使用Android Image Asset Studio生成一组图标。但是,我不知道如何在Cordova
中将这些图标设置到我的应用程序中。
当关注documentation regarding icons in Cordova时,我只设法使用以下代码将方形图标设置为我的项目:
<platform name="android">
<!--
ldpi : 36x36 px
mdpi : 48x48 px
hdpi : 72x72 px
xhdpi : 96x96 px
xxhdpi : 144x144 px
xxxhdpi : 192x192 px
-->
<icon src="res/android/ldpi.png" density="ldpi" />
<icon src="res/android/mdpi.png" density="mdpi" />
<icon src="res/android/hdpi.png" density="hdpi" />
<icon src="res/android/xhdpi.png" density="xhdpi" />
<icon src="res/android/xxhdpi.png" density="xxhdpi" />
<icon src="res/android/xxxhdpi.png" density="xxxhdpi" />
</platform>
但是,在Android Oreo中,应用程序的图标是圆形的,它不能在该手机上正确显示我的应用程序图标。图标在圆圈内缩小,周围有白色背景。
问题:如何将Image Asset Studio生成的圆形图标设置为Cordova项目?
以下是我正在制作的项目的经过测试和运行的解决方案。
将所有生成的图标复制到项目根目录的res/android
(与resources
或platforms
文件夹相同),并将以下配置添加到config.xml
文件中:
<widget xmlns:android="http://schemas.android.com/apk/res/android">
<platform name="android">
<edit-config file="app/src/main/AndroidManifest.xml" mode="merge" target="/manifest/application">
<application android:icon="@mipmap/ic_launcher" android:roundIcon="@mipmap/ic_launcher_round" />
</edit-config>
<resource-file src="res/android/drawable/ic_launcher_background.xml" target="app/src/main/res/drawable/ic_launcher_background.xml" />
<resource-file src="res/android/mipmap-hdpi/ic_launcher.png" target="app/src/main/res/mipmap-hdpi/ic_launcher.png" />
<resource-file src="res/android/mipmap-hdpi/ic_launcher_round.png" target="app/src/main/res/mipmap-hdpi/ic_launcher_round.png" />
<resource-file src="res/android/mipmap-mdpi/ic_launcher.png" target="app/src/main/res/mipmap-mdpi/ic_launcher.png" />
<resource-file src="res/android/mipmap-mdpi/ic_launcher_round.png" target="app/src/main/res/mipmap-mdpi/ic_launcher_round.png" />
<resource-file src="res/android/mipmap-xhdpi/ic_launcher.png" target="app/src/main/res/mipmap-xhdpi/ic_launcher.png" />
<resource-file src="res/android/mipmap-xhdpi/ic_launcher_round.png" target="app/src/main/res/mipmap-xhdpi/ic_launcher_round.png" />
<resource-file src="res/android/mipmap-xxhdpi/ic_launcher.png" target="app/src/main/res/mipmap-xxhdpi/ic_launcher.png" />
<resource-file src="res/android/mipmap-xxhdpi/ic_launcher_round.png" target="app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png" />
<resource-file src="res/android/mipmap-xxxhdpi/ic_launcher.png" target="app/src/main/res/mipmap-xxxhdpi/ic_launcher.png" />
<resource-file src="res/android/mipmap-xxxhdpi/ic_launcher_round.png" target="app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png" />
</platform>
</widget>
不要忘记将xmlns:android="http://schemas.android.com/apk/res/android"
添加到你的<widget>
。
如果你有一个<icon>
=> <widget>
=> <platform
,请删除<icon>
。
将以上更改添加到config.xml
后,使用ionic cordova platform remove android
或sudo ionic cordova platform remove android
删除您的Android平台(取决于您的环境设置),然后使用ionic cordova platform add android
或sudo ionic cordova platform add android
再次添加Android平台。
创建构建,安装并检查结果。
我在生产代码中使用了以上配置,结果如下:
您可以尝试这样做:从图像资源中选择应用程序图标的图像后,将Shape的属性(在图像资产下的传统选项卡中找到)从Square设置为None。
<splash platform="android" src="package-assets/splash_320_426.png" density="ldpi" width="320" height="426" orientation="portrait"/>
您可以将android更改为ios,将src =“path”更改为您想要的任何值,将密度更改为其中一个已知设置,设置图像宽度和高度以及方向。图标方向无关紧要,但飞溅和其他图像可能不相关。图标设置如下:
<icon platform="android" src="package-assets/ldpi.png" density="ldpi" width="36" height="36"/>
当然,这是在config.xml中,因为您在标记中指定了平台,所以您不必将其放在平台部分中。
当你使用谷歌“Cordova Android自适应图标”时,这个SO帖子是最受欢迎的。这里建议的方法,特别是@VicJordan的答案是一个完整的解决方案。但是,应该注意的是,Cordova Android的version 8引入了自己的支持自适应图标的方式,不需要您使用Android Asset Studio。
这是你需要做的
<icon density="?dpi" src = "path/to/icon/resource"/>
文件中用于Cordova应用程序的旧样式config.xml
语句<icon density = "?dpi" background = "path/to/icon/background"/>
指令<icon density = "?dpi" background="path/to/icon/foreground"/>
指令哪里有? = l|m|h|x|xx|xxx
您也可以使用颜色黑色而不是图像。有关所有这些的详细信息,请参阅Cordova 8 documentation。