Android Oreo-如何在Cordova中设置自适应图标?

Android Oreo-如何在Cordova中设置自适应图标?,android,cordova,Android,Cordova,只是想知道是否有人能够为Android Oreo在Cordova上设置自适应图标?我使用的是安卓6.4.0,我的方形图标缩小以适应圆形。我只是不想让它缩水。我不在乎这些角是否从圆角处剪掉。您首先需要将Cordova更新为7.0。支持Android Oreo。一旦您使用Cordova 7创建了一个应用程序,那么您就必须使用本机Android代码手动创建自适应图标。这将有助于你做到这一点。一旦创建了,您就可以将它们添加到config.xml中,如下所示- <platform name="and

只是想知道是否有人能够为Android Oreo在Cordova上设置自适应图标?我使用的是安卓6.4.0,我的方形图标缩小以适应圆形。我只是不想让它缩水。我不在乎这些角是否从圆角处剪掉。

您首先需要将Cordova更新为7.0。支持Android Oreo。一旦您使用Cordova 7创建了一个应用程序,那么您就必须使用本机Android代码手动创建自适应图标。这将有助于你做到这一点。一旦创建了,您就可以将它们添加到
config.xml
中,如下所示-

<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>

据我所知,Cordova尚未设置自适应图标。但是,在运行Cordova build之后,手动执行该操作非常简单

将AndroidManifest.xml中的android:图标更改为:

<application android:hardwareAccelerated="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:supportsRtl="true">  


你走开!我希望Cordova能很快将此应用到他们的产品中。

警告:不要使用此答案。从Cordova 9开始,这一点现在得到了开箱即用的支持。请参见

我按照中所述创建了图标,将它们复制到
res/android
,并使用以下配置:

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/drawable/ic_launcher_foreground.xml" target="app/src/main/res/drawable/ic_launcher_foreground.xml" />
        <resource-file src="res/android/mipmap-anydpi-v26/ic_launcher.xml" target="app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml" />
        <resource-file src="res/android/mipmap-anydpi-v26/ic_launcher_round.xml" target="app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.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>
<platform name="android">
        <resource-file src="res/icons/android/colors.xml" target="/app/src/main/res/values/colors.xml" />
        <icon density="ldpi" background="@color/background" foreground="res/icons/android/ldpi-foreground.png" />
        <icon density="mdpi" background="@color/background" foreground="res/icons/android/mdpi-foreground.png" />
        <icon density="hdpi" background="@color/background" foreground="res/icons/android/hdpi-foreground.png" />
        <icon density="xhdpi" background="@color/background" foreground="res/icons/android/xhdpi-foreground.png" />
        <icon density="xxhdpi" background="@color/background" foreground="res/icons/android/xxhdpi-foreground.png" />
        <icon density="xxxhdpi" background="@color/background" foreground="res/icons/android/xxxhdpi-foreground.png" />
    </platform>


警告:不要使用此答案。从Cordova 9开始,这一点现在得到了开箱即用的支持。请参见

因此,尽管上述答案帮助我找到了答案,但它们要么过时,要么不完整。因此,为了帮助任何人向前迈进,这是一个完整的答案,包括我能想到的所有细节

步骤1:创建图标

您将希望使用Image Asset Studio()完成此操作。有很多关于这样做的指南

步骤2:将图标移动到爱奥尼亚/cordova项目中

将整个
res
文件夹复制到项目中。以下示例适用于ionic v1

cp -a AndroidStudioProjects/MyApplication4/app/src/main/res MyIonicProject/myapp/resources/android/adaptiveicon
步骤3:编辑config.xml

首先,要使用图标(其他答案中缺少此图标),您需要更改顶部的
小部件
行。您需要向它添加
xmlns:android=“schemas.android.com/apk/res/android”
,因此它看起来如下所示。这允许系统更改
AndroidMenifest.xml
文件

<widget id="io.ionic.starter" version="0.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:android="schemas.android.com/apk/res/android" xmlns:cdv="http://cordova.apache.org/ns/1.0">
最后,对于新
resources/android/adaptiveicon
文件夹中的每个文件,您需要添加一行如下内容:

<resource-file src="resources/android/adaptiveicon/<folder>/<file>" target="app/src/main/res/<folder>/<file>" />
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="background">#FFFFFF</color>
</resources>
第4步:安全起见,清洁Android平台

运行以下命令以清理平台

cd myapp
rm -rf platforms/android
ionic cordova prepare
为了更好地解决问题,请在爱奥尼亚的Android emulator启动中修复错误:

wget https://raw.githubusercontent.com/gegenokitaro/cordova-android/8d497784ac4a40a9689e616cd486c4ed07d3e063/bin/templates/cordova/lib/emulator.js -O platforms/android/cordova/lib/emulator.js
第5步:构建

建造:

ionic cordova build android
或模仿:

ionic cordova emulate android --consolelogs --serverlogs --target "Android8"

这里是爱奥尼亚v4和Cordova Android的更新:6.4.0。
0x6368656174
答案的显著变化是:

  • 正在从资源目标中删除
    app/src/main
  • 我的background.xml位于
    values
    文件夹中(我使用png时没有前景)
  • edit-config
    Manifest文件位置位于同一目录
    file=“AndroidManifest.xml”
我挣扎了几天,但这对我来说是有效的:

config.xml

<widget ... xmlns:android="http://schemas.android.com/apk/res/android">
    <platform name="android">
    <resource-file src="resources/android/values/ic_launcher_background.xml" target="res/values/ic_launcher_background.xml" />
    <resource-file src="resources/android/mipmap-anydpi-v26/ic_launcher.xml" target="res/mipmap-anydpi-v26/ic_launcher.xml" />
    <resource-file src="resources/android/mipmap-anydpi-v26/ic_launcher_round.xml" target="res/mipmap-anydpi-v26/ic_launcher_round.xml" />
    <resource-file src="resources/android/mipmap-hdpi/ic_launcher.png" target="res/mipmap-hdpi/ic_launcher.png" />
    <resource-file src="resources/android/mipmap-hdpi/ic_launcher_round.png" target="res/mipmap-hdpi/ic_launcher_round.png" />
    <resource-file src="resources/android/mipmap-mdpi/ic_launcher.png" target="res/mipmap-mdpi/ic_launcher.png" />
    <resource-file src="resources/android/mipmap-mdpi/ic_launcher_round.png" target="res/mipmap-mdpi/ic_launcher_round.png" />
    <resource-file src="resources/android/mipmap-xhdpi/ic_launcher.png" target="res/mipmap-xhdpi/ic_launcher.png" />
    <resource-file src="resources/android/mipmap-xhdpi/ic_launcher_round.png" target="res/mipmap-xhdpi/ic_launcher_round.png" />
    <resource-file src="resources/android/mipmap-xxhdpi/ic_launcher.png" target="res/mipmap-xxhdpi/ic_launcher.png" />
    <resource-file src="resources/android/mipmap-xxhdpi/ic_launcher_round.png" target="res/mipmap-xxhdpi/ic_launcher_round.png" />
    <resource-file src="resources/android/mipmap-xxxhdpi/ic_launcher.png" target="res/mipmap-xxxhdpi/ic_launcher.png" />
    <resource-file src="resources/android/mipmap-xxxhdpi/ic_launcher_round.png" target="res/mipmap-xxxhdpi/ic_launcher_round.png" />
    <edit-config file="AndroidManifest.xml" mode="merge" target="/manifest/application">
         <application android:icon="@mipmap/ic_launcher" android:roundIcon="@mipmap/ic_launcher_round" />
    </edit-config>
    </platform>    
</widget>


PHEW

我可能会迟到,但我很难让它正常工作,因为(a)使用PhoneGap构建,以及(b)手工操作,而不是使用Android Studio。因此,对于那些在家里玩游戏的人来说,我要做的就是让自适应图标正常工作:

  • 在我的
    config.xml
    中的
    中,我设置:
  • 
    
    有助于说明您必须在
    上使用不同的
    目标路径这一事实,Cordova Android 8.0.0现在支持这一点。请参阅和

    例如,在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/drawable/ic_launcher_foreground.xml" target="app/src/main/res/drawable/ic_launcher_foreground.xml" />
            <resource-file src="res/android/mipmap-anydpi-v26/ic_launcher.xml" target="app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml" />
            <resource-file src="res/android/mipmap-anydpi-v26/ic_launcher_round.xml" target="app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.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>
    
    <platform name="android">
            <resource-file src="res/icons/android/colors.xml" target="/app/src/main/res/values/colors.xml" />
            <icon density="ldpi" background="@color/background" foreground="res/icons/android/ldpi-foreground.png" />
            <icon density="mdpi" background="@color/background" foreground="res/icons/android/mdpi-foreground.png" />
            <icon density="hdpi" background="@color/background" foreground="res/icons/android/hdpi-foreground.png" />
            <icon density="xhdpi" background="@color/background" foreground="res/icons/android/xhdpi-foreground.png" />
            <icon density="xxhdpi" background="@color/background" foreground="res/icons/android/xxhdpi-foreground.png" />
            <icon density="xxxhdpi" background="@color/background" foreground="res/icons/android/xxxhdpi-foreground.png" />
        </platform>
    
    
    
    colors.xml如下所示:

    <resource-file src="resources/android/adaptiveicon/<folder>/<file>" target="app/src/main/res/<folder>/<file>" />
    
    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <color name="background">#FFFFFF</color>
    </resources>
    
    
    #FFFFFF
    
    警告:不要使用此答案。从Cordova 9开始,这一点现在得到了开箱即用的支持。请参见

    最近的安卓系统使用自适应图标,该图标具有背景和前景图像以及一些xml文件。这就是我在ionic应用程序中设置自适应图标所做的:

  • config.xml
    中,我将
    android minSdkVersion
    设置为版本26
  • 接下来,我必须创建Android自适应图标。为此,我使用了作为Android Studio一部分的图像资产。首先,我从photoshop中创建了2个背景图像和透明图标图像,用作png格式的前景。之后,我执行以下步骤来生成图标:
    • 打开Android Studio并创建新项目或打开现有项目

    • 单击左侧边栏中的app->res。右键单击res->New->Image Assets

    • 所选前景层->资产类型“图像”&仅带有图标的图标图像和透明背景的所选路径。然后选择“修剪”为“是”,并根据需要调整大小

    • 所选背景层->资产类型“图像”和所选路径。(或者,您甚至可以设置“颜色”)

    • 单击下一步并单击“完成”

    • 现在,我右键点击res文件夹并选择“在Finder中显示”

    • 我复制了res文件夹中的所有文件夹,并将其放入:
      myapp/resources/android/adaptiveicon/

  • 接下来,我只需要将以下代码添加到config.xml。确保每个文件在
            <icon density="ldpi" src="resources/android/icon/drawable-ldpi-icon.png" />
            <icon density="mdpi" src="resources/android/icon/drawable-mdpi-icon.png" />
            <icon density="hdpi" src="resources/android/icon/drawable-hdpi-icon.png" />
            <icon density="xhdpi" src="resources/android/icon/drawable-xhdpi-icon.png" />
            <icon density="xxhdpi" src="resources/android/icon/drawable-xxhdpi-icon.png" />
            <icon density="xxxhdpi" src="resources/android/icon/drawable-xxxhdpi-icon.png" />
    
            <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="resources/android/adaptiveicon/mipmap-anydpi-v26/ic_launcher.xml" target="app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml" />
            <resource-file src="resources/android/adaptiveicon/mipmap-anydpi-v26/ic_launcher_round.xml" target="app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml" />
            <resource-file src="resources/android/adaptiveicon/mipmap-hdpi/ic_launcher.png" target="app/src/main/res/mipmap-hdpi/ic_launcher.png" />
            <resource-file src="resources/android/adaptiveicon/mipmap-hdpi/ic_launcher_round.png" target="app/src/main/res/mipmap-hdpi/ic_launcher_round.png" />
            <resource-file src="resources/android/adaptiveicon/mipmap-hdpi/ic_launcher_background.png" target="app/src/main/res/mipmap-hdpi/ic_launcher_background.png" />
            <resource-file src="resources/android/adaptiveicon/mipmap-hdpi/ic_launcher_foreground.png" target="app/src/main/res/mipmap-hdpi/ic_launcher_foreground.png" />
            <resource-file src="resources/android/adaptiveicon/mipmap-mdpi/ic_launcher.png" target="app/src/main/res/mipmap-mdpi/ic_launcher.png" />
            <resource-file src="resources/android/adaptiveicon/mipmap-mdpi/ic_launcher_round.png" target="app/src/main/res/mipmap-mdpi/ic_launcher_round.png" />
            <resource-file src="resources/android/adaptiveicon/mipmap-mdpi/ic_launcher_background.png" target="app/src/main/res/mipmap-mdpi/ic_launcher_background.png" />
            <resource-file src="resources/android/adaptiveicon/mipmap-mdpi/ic_launcher_foreground.png" target="app/src/main/res/mipmap-mdpi/ic_launcher_foreground.png" />
            <resource-file src="resources/android/adaptiveicon/mipmap-xhdpi/ic_launcher.png" target="app/src/main/res/mipmap-xhdpi/ic_launcher.png" />
            <resource-file src="resources/android/adaptiveicon/mipmap-xhdpi/ic_launcher_round.png" target="app/src/main/res/mipmap-xhdpi/ic_launcher_round.png" />
            <resource-file src="resources/android/adaptiveicon/mipmap-xhdpi/ic_launcher_background.png" target="app/src/main/res/mipmap-xhdpi/ic_launcher_background.png" />
            <resource-file src="resources/android/adaptiveicon/mipmap-xhdpi/ic_launcher_foreground.png" target="app/src/main/res/mipmap-xhdpi/ic_launcher_foreground.png" />
            <resource-file src="resources/android/adaptiveicon/mipmap-xxhdpi/ic_launcher.png" target="app/src/main/res/mipmap-xxhdpi/ic_launcher.png" />
            <resource-file src="resources/android/adaptiveicon/mipmap-xxhdpi/ic_launcher_round.png" target="app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png" />
            <resource-file src="resources/android/adaptiveicon/mipmap-xxhdpi/ic_launcher_background.png" target="app/src/main/res/mipmap-xxhdpi/ic_launcher_background.png" />
            <resource-file src="resources/android/adaptiveicon/mipmap-xxhdpi/ic_launcher_foreground.png" target="app/src/main/res/mipmap-xxhdpi/ic_launcher_foreground.png" />
            <resource-file src="resources/android/adaptiveicon/mipmap-xxxhdpi/ic_launcher.png" target="app/src/main/res/mipmap-xxxhdpi/ic_launcher.png" />
            <resource-file src="resources/android/adaptiveicon/mipmap-xxxhdpi/ic_launcher_round.png" target="app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png" />
            <resource-file src="resources/android/adaptiveicon/mipmap-xxxhdpi/ic_launcher_background.png" target="app/src/main/res/mipmap-xxxhdpi/ic_launcher_background.png" />
            <resource-file src="resources/android/adaptiveicon/mipmap-xxxhdpi/ic_launcher_foreground.png" target="app/src/main/res/mipmap-xxxhdpi/ic_launcher_foreground.png" />