首页 > 解决方案 > Cordova - Android 上的自适应图标

问题描述

我有一组使用Android Image Asset Studio生成的图标。但是,我不知道如何将这些图标设置到我的应用程序中Cordova

在遵循有关 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 项目?

标签: androidcordovaiconsfill

解决方案


以下是我正在生产的项目的经过测试且有效的解决方案。

将所有生成的图标复制到res/android项目的根目录(与文件夹相同的级别)并将以下配置添加到resources文件中:platformsconfig.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>

将上述更改添加到您的 后,使用orconfig.xml删除您的 Android 平台(取决于您的环境设置),然后使用or再次添加 Android 平台。ionic cordova platform remove androidsudo ionic cordova platform remove androidionic cordova platform add androidsudo ionic cordova platform add android

创建构建,安装并检查结果。

我在生产代码中使用了上述配置,结果如下:

在此处输入图像描述


推荐阅读