【问题标题】:Android Oreo - how do I set Adaptive Icons in Cordova?Android Oreo - 如何在 Cordova 中设置自适应图标?
【发布时间】:2017-11-28 03:39:29
【问题描述】:

只是想知道是否有人能够在 Cordova for Android Oreo 上设置自适应图标?我正在使用 android 6.4.0,我的方形图标缩小以适合圆圈。我只是希望它不缩小。我不在乎圆角是否被剪掉。

【问题讨论】:

  • @CharliePrynn 我的回答有帮助吗?
  • 感谢您的帮助@CharliePrynn。我必须在我们应用的下一个版本中对其进行测试。
  • 我已尝试执行建议的答案,但我的图标仍显示在白色框内,而不是填充 who 图标。我的背景在 android studio 的 drawable-v24 中生成。有什么想法吗?

标签: android cordova


【解决方案1】:

警告:请勿使用此答案。现在从 Cordova 9 开始支持开箱即用。请参阅 https://stackoverflow.com/a/55169307/2947592

我按照https://developer.android.com/studio/write/image-asset-studio.html#create-adaptive 中的描述创建了图标,将它们复制到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>

【讨论】:

  • 我使用 Android Studio 创建了资产,复制到了 Phonegap 项目根目录的 res/android,将您提供的 xml 添加到 config.xml,我的图标仍然显示为白色内的一个小方形图标- 像以前一样的背景圆圈。我正在使用 Phonegap 7.1.1。有什么想法我可能做错了吗?
  • 我必须从 target 中删除 app/src/main 才能正常工作。
  • 我必须将 xmlns:android="http://schemas.android.com/apk/res/android" 添加到 config.xml 的小部件标签中才能使其正常工作。
  • 如果您选择背景颜色而不是图像,则第一个资源文件将是 &lt;resource-file src="res/android/values/ic_launcher_background.xml" target="app/src/main/res/values/ic_launcher_background.xml" /&gt;(值而不是可绘制的)。感谢您发布此解决方案,真的帮了我大忙!
  • 我使用我的旧应用程序 icon.png 作为我的foreground 资产图像,并使用color:white 作为我的背景。但是,当 Image Asset 生成所有内容时,它不会生成 icon_launcher_foreground.xml 文件 - 如果没有此文件,我的构建将失败,因为 ic_launcher.xml 和 ic_launcher_round.xml 都引用了 foregound.xml 文件。它确实会生成所有ic_launcher_foreground.png 图像。如果我删除foreground.xml 引用,我的应用程序会构建,但图标只是白色背景。我开始了一个新项目并得到了相同的结果,我在这里缺少什么魔力?
【解决方案2】:

警告:请勿使用此答案。现在从 Cordova 9 开始支持开箱即用。请参阅 https://stackoverflow.com/a/55169307/2947592

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

第 1 步:创建图标

您需要使用 Image Asset Studio (https://developer.android.com/studio/write/image-asset-studio) 来执行此操作。有许多关于这样做的指南。

第 2 步:将图标移动到您的 ionic/cordova 项目中

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

cp -a AndroidStudioProjects/MyApplication4/app/src/main/res MyIonicProject/myapp/resources/android/adaptiveicon

第 3 步:编辑 config.xml

首先,要使用图标(其他答案中缺少此图标),您需要更改顶部的 widget 行。您需要将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">

接下来,您需要调整 config.xml 的平台部分。

首先从&lt;platform name="android"&gt; 部分中删除&lt;icon density= ... /&gt; 的所有实例。

然后,将更改添加到Android Manifest 文件:

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

最后,对于新 resources/android/adaptiveicon 文件夹中的每个文件,您需要添加如下一行:

<resource-file src="resources/android/adaptiveicon/<folder>/<file>" target="app/src/main/res/<folder>/<file>" />

确保每个文件都被表示!您最后的 platform 部分可能如下所示(此示例适用于前景和背景都使用 PNG 时):

<platform name="android">
    <allow-intent href="market:*" />
    <splash density="land-ldpi" src="resources/android/splash/drawable-land-ldpi-screen.png" />
    <splash density="land-mdpi" src="resources/android/splash/drawable-land-mdpi-screen.png" />
    <splash density="land-hdpi" src="resources/android/splash/drawable-land-hdpi-screen.png" />
    <splash density="land-xhdpi" src="resources/android/splash/drawable-land-xhdpi-screen.png" />
    <splash density="land-xxhdpi" src="resources/android/splash/drawable-land-xxhdpi-screen.png" />
    <splash density="land-xxxhdpi" src="resources/android/splash/drawable-land-xxxhdpi-screen.png" />
    <splash density="port-ldpi" src="resources/android/splash/drawable-port-ldpi-screen.png" />
    <splash density="port-mdpi" src="resources/android/splash/drawable-port-mdpi-screen.png" />
    <splash density="port-hdpi" src="resources/android/splash/drawable-port-hdpi-screen.png" />
    <splash density="port-xhdpi" src="resources/android/splash/drawable-port-xhdpi-screen.png" />
    <splash density="port-xxhdpi" src="resources/android/splash/drawable-port-xxhdpi-screen.png" />
    <splash density="port-xxxhdpi" src="resources/android/splash/drawable-port-xxxhdpi-screen.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/drawable/ic_launcher_background.xml" target="app/src/main/res/drawable/ic_launcher_background.xml" />
    <resource-file src="resources/android/adaptiveicon/drawable-v24/ic_launcher_foreground.xml" target="app/src/main/res/drawable-v24/ic_launcher_foreground.xml" />
    <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_foreground.png" target="app/src/main/res/mipmap-hdpi/ic_launcher_foreground.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_round.png" target="app/src/main/res/mipmap-hdpi/ic_launcher_round.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_foreground.png" target="app/src/main/res/mipmap-mdpi/ic_launcher_foreground.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_round.png" target="app/src/main/res/mipmap-mdpi/ic_launcher_round.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_foreground.png" target="app/src/main/res/mipmap-xhdpi/ic_launcher_foreground.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_round.png" target="app/src/main/res/mipmap-xhdpi/ic_launcher_round.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_foreground.png" target="app/src/main/res/mipmap-xxhdpi/ic_launcher_foreground.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_round.png" target="app/src/main/res/mipmap-xxhdpi/ic_launcher_round.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_foreground.png" target="app/src/main/res/mipmap-xxxhdpi/ic_launcher_foreground.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_round.png" target="app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png" />
</platform>

第 4 步:安全起见,清理 Android 平台

运行以下命令清理平台。

cd myapp
rm -rf platforms/android
ionic cordova prepare

为了更好的措施,请在 ionic 中修复 Android 模拟器启动的错误:

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"

【讨论】:

  • 您确定第 3 步仍然是最新的吗?创建和测试图标后,我在清单中的任何地方都找不到以下内容:android:roundIcon="@mipmap/ic_launcher_round" 所以我相信没有它也可以工作。
【解决方案3】:

Cordova Android 8.0.0 现在支持此功能。请参阅announcement 和documentation。

例如,在 config.xml 中定义如下图标:

<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 看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="background">#FFFFFF</color>
</resources>

【讨论】:

    【解决方案4】:

    警告:请勿使用此答案。现在从 Cordova 9 开始支持开箱即用。请参阅 https://stackoverflow.com/a/55169307/2947592

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

    1. 在config.xml 中,我将android-minSdkVersion 设置为版本26。
    <preference name="android-minSdkVersion" value="26" />
    <preference name="android-targetSdkVersion" value="28" />
    
    1. 在config.xml 中,删除icon density 标记并删除这些行:
            <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" />
    
    1. 接下来,我必须创建 Android 自适应图标。为此,我使用了作为 Android Studio 一部分的图像资源。起初,我从 photoshop 创建了 2 个背景图像和仅透明图标图像,以用作 png 格式的前景。之后,我执行了以下步骤来生成图标:
    • 打开 Android Studio 并创建一个新项目或打开现有项目。

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

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

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

    • 点击下一步,然后点击“完成”。

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

    • 我把res文件夹里面的所有文件夹都复制了,放到里面:my-app/resources/android/adaptiveicon/

    1. 接下来,我只需要将以下代码添加到 config.xml 中。确保adaptiveicon 文件夹中的每个文件都正确链接到此处,否则在构建过程中会抛出“未找到”错误。
            <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" />
    

    就是这样。现在应用程序将具有自适应图标。

    【讨论】:

    • 非常感谢您的详细解释。信息:如果您选择仅背景颜色方式,请从 config.xml 中删除 ic_launcher_background_png 条目。您还需要添加以下行以使构建工作&lt;resource-file src="resources/android/adaptiveicon/values/ic_launcher_background.xml" target="app/src/main/res/values/ic_launcher_background.xml" /&gt;
    • 只需在此处删除 Source/platforms/android/app/src/main/res 以下目录:mipmap-ldpi、mipmap-ldpi-v26、mipmap-mdpi-v26、mipmap-hdpi-v26、mipmap-xhdpi-v26、mipmap-xxhdpi-v26 , mipmap-xxxhdpi-v26
    • @StefanRein 非常感谢这些有用的 cmets #lifesavior。
    • 感谢@StefanRein 很高兴我的回答有帮助。
    • 只需将两种解决方案结合起来:“cordova 资源”中的 与手册 的东西。
    【解决方案5】:

    据我所知,Cordova 还没有设置自适应图标。但是在您运行 Cordova 构建之后手动执行很容易。

    将 AndroidManifest.xml 中的 android:icon 更改为:

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

    在 res/drawable 中创建 ic_launcher.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
        <background android:drawable="@drawable/ic_launcher_background"/>
        <foreground android:drawable="@drawable/ic_launcher_foreground"/>
    </adaptive-icon>
    

    然后在 res/drawable 中添加你的两个矢量文件 ic_launcher_background.xml 和 ic_launcher_foreground.xml。这些可以使用此工具创建:http://inloop.github.io/svg2android/

    你走吧!我希望 Cordova 尽快将其融入到他们的构建中。

    【讨论】:

    • 它工作正常。 AndroidManifest.xml 也可以使用以下命令在构建 shell 脚本中轻松更改 sed -i '' 's#@mipmap/icon#@drawable/ic_launcher#' platforms/android/AndroidManifest.xml
    • 更多测试:它只适用于奥利奥设备,不适用于使用默认安卓图标的旧平台。
    【解决方案6】:

    Ionic v4 和 Cordova Android 的更新如下:6.4.0。 0x6368656174 的答案的显着变化是:

    • 从资源目标中删除 app/src/main。
    • 我的 background.xml 位于 values 文件夹中(我没有前景,因为我使用的是 png)
    • edit-config 清单文件位置在同一目录中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>
    

    呸

    【讨论】:

      【解决方案7】:

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

      1. 在我的config.xml 中的&lt;platform name="android"&gt; 中,我设置了:
              <resource-file src="www/pwa/android/icon-bg.png"               target="app/src/main/res/mipmap/ic_bg.png" />
              <resource-file src="www/pwa/android/icon-fg.png"               target="app/src/main/res/mipmap/ic_fg.png" />
              <resource-file src="www/pwa/ic/mipmap-xxxhdpi/ic_launcher.png" target="app/src/main/res/mipmap/ic_launcher.png" />
              <resource-file src="www/pwa/ic/mipmap-xxxhdpi/ic_launcher.png" target="app/src/main/res/mipmap/ic_launcher_round.png" />
              <resource-file src="www/pwa/android/ic_launcher.xml"           target="app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml" />
              <resource-file src="www/pwa/android/ic_launcher_round.xml"     target="app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml" />
      
              <!-- Per https://forums.adobe.com/thread/2576077 -->
              <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>
      
      1. 上述配置中的 PNG 文件是不言自明的。引用为 ic_launcher.xml 和 ic_launcher_round.xml 的 XML 文件是相同的,我只是在源位置创建了这个文件,并通过上面的资源标签将它复制了进去。这是这两个 XML 文件的内容,引用为 src public/pwa/android/ic_launcher.xml 和 ic_launcher_round.xml:
      <?xml version="1.0" encoding="utf-8"?>
      <adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
          <background android:drawable="@mipmap/ic_bg"/>
          <foreground android:drawable="@mipmap/ic_fg"/>
      </adaptive-icon>
      

      注意我的目标是 phonegap 版本 8.1.1 (&lt;preference name="phonegap-version" value="cli-8.1.1" /&gt;) https://forums.adobe.com/thread/2576077 的帖子有助于说明您必须在 @ 上使用不同的 target 路径这一事实987654332@ 标签,取决于您使用的cli 版本。

      希望这会有所帮助,如果我遗漏了什么,请向我提问。干杯!

      【讨论】:

      • 谢谢!最后,我能够将 AI 添加到我的 PGB 项目中!!
      【解决方案8】:

      安卓 图标-android-foreground.png 432*432 像素 72dpi

      ios 不透明的png 72dpi

      Android 参考 https://developer.android.com/guide/practices/ui_guidelines/icon_design_adaptive

        const CordovaRes = require('cordova-res');
        const iconFolder = {
          dev: 'test',
          test: 'test',
          beta: 'beta',
          betaInternal: 'beta',
          betaRelease: 'beta',
          production: 'production'
        }[ENV];
        const sourcePath = `resources/_environments/${iconFolder}/`;
        const options = {
          resourcesDirectory: 'resources',
          platforms: {
            ios: { icon: { sources: [`${sourcePath}/icon-ios.png`] } },
            android: {
              'adaptive-icon': {
                icon: { sources: [`${sourcePath}/icon-android.png`] },
                foreground: { sources: [`${sourcePath}/icon-android-foreground.png`] },
                background: {
                  sources: [{ type: 'color', color: '#b5c429' }]
                }
              }
            }
          }
        };
       CordovaRes(options);

      【讨论】:

        【解决方案9】:

        您首先需要将 Cordova 更新到 7.0。 Cordova 7.0 支持 Android Oreo。使用 Cordova 7 创建应用程序后,您必须使用原生 Android 代码手动创建自适应图标。这个Medium blog 会帮助你。创建Adaptive Icons 后,您可以像这样将它们添加到您的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>
        

        【讨论】:

        • 自适应图标使用前景和背景 .xml 文件,可以通过 Image Asset Studio 生成。这些文件是如何通过 config.xml 加载到 Cordova 中的?
        猜你喜欢
        • 1970-01-01
        • 2020-10-10
        • 2017-05-25
        • 1970-01-01
        • 2015-01-25
        • 1970-01-01
        • 2018-08-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多