图标切换器
图标切换器是一个插件,可以轻松地在应用内更改应用的图标。请看下面的简短演示。
演示
如何使用
iOS
点击展开
先决条件
- 重要提示: 此包不处理初始应用图标。您必须自己提供/设置它。
- iOS 10.3 及以上版本。
- 图片的 @1x, @2x, 和 @3x 大小,就像标准的 iOS 图片一样。您可以在 此处 找到一个用于此的工具(也可用于 Android)。
准备工作
- 在 Xcode 中打开项目中的
Runner.xcworkspace。 - 在 Runner 文件夹中创建一个名为
App Icons的文件夹。 - 在此文件夹中,按如下方式放置您的应用图标:
然后,按所示修改 info.plist。
...
<plist version="1.0">
<dict>
<key>CFBundleIcons</key>
<dict>
<key>CFBundleAlternateIcons</key>
<dict>
<key>NAME</key>
<dict>
<key>UIPrerenderedIcon</key>
<string>NO</string>
<key>CFBundleIconFiles</key>
<array>
<string>NAME</string>
</array>
</dict>
<key>NAME2</key>
<dict>
<key>UIPrerenderedIcon</key>
<string>NO</string>
<key>CFBundleIconFiles</key>
<array>
<string>NAME2</string>
</array>
</dict>
</dict>
</dict>
...
将 NAME 和 NAME2 替换为您应用图标文件的名称。
之后,您就可以开始了!
Android
点击展开
先决条件
- 重要提示:Android 默认不提供像 iOS 那样更改应用图标的解决方案,所以这种方式可能不是最简洁最好的,但它有效。
- 您需要知道您的
applicationId,默认情况下它是:com.example.example - mipmap 格式的图片,就像默认的 Android 图标一样。查找用于此的工具:此处(也可用于 iOS 图标)。
准备
- 将您的图片放入目标文件夹:
your_project/android/app/src/main/res/mipmap-anydpi-v26,your_project/android/app/src/main/res/mipmap-hdpi,your_project/android/app/src/main/res/mipmap-mdpi,your_project/android/app/src/main/res/mipmap-xhdpi,your_project/android/app/src/main/res/mipmap-xxhdpi和your_project/android/app/src/main/res/mipmap-xxxhdpi。
转到 your_project/android/app/src/main/AndroidManifest.xml,您需要为每个图标添加如下条目:
...
</activity>
<!--START HERE: -->
<activity-alias
android:name=".NameOfYourActivity"
android:targetActivity=".MainActivity"
android:icon="@mipmap/NameOfTheMipMapImage"
android:enabled="false">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity-alias>
...
NameOfTheMipMapImage 必须仅仅是您放入 mipmap 文件夹的图片的名称(请随时查看我的示例)。
NameOfYourActivity 非常重要!因为现在我们需要在我们的 Android 项目中创建一个 Activity Class,为此我编辑了位于 your_project/android/app/src/main/kotlin/com/example/example/MainActivity.kt 的 MainActivity.kt 文件。在此文件中,您需要创建新的 Activity 类,其名称与您在 NameOfYourActivity 中指定的名称相同。
示例
我的 AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application
android:label="example"
android:name="${applicationName}"
android:icon="@mipmap/ic_launcher">
<activity
android:name=".MainActivity"
android:exported="true"
android:launchMode="singleTop"
android:theme="@style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
<!-- Specifies an Android theme to apply to this Activity as soon as
the Android process has started. This theme is visible to the user
while the Flutter UI initializes. After that, this theme continues
to determine the Window background behind the Flutter UI. -->
<meta-data
android:name="io.flutter.embedding.android.NormalTheme"
android:resource="@style/NormalTheme"
/>
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<!--IMPORTANT START HERE: -->
<activity-alias
android:name=".First"
android:targetActivity=".MainActivity"
android:icon="@mipmap/first"
android:enabled="false">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity-alias>
<!-- Don't delete the meta-data below.
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
<meta-data
android:name="flutterEmbedding"
android:value="2" />
</application>
</manifest>
如您所见,我需要创建一个名为 First 的类,如下所示:
package com.example.example
import io.flutter.embedding.android.FlutterActivity
class MainActivity: FlutterActivity() {
}
//NEW:
class First: FlutterActivity() {
}
Flutter/Dart 部分
可惜的是,我们也需要为 Android 设置一个不同的东西。
IconSwitcher.setDefaultComponent("com.example.example.MainActivity");
我们需要在应用启动时调用此方法,也请随意查看我的示例应用程序,看看我是如何做的。这里重要的是,applicationId 和 MainActivity 名称必须正确,请仔细检查以防出错。
在 Flutter 应用中使用 Icon_Switcher
要切换图标,在完成上述 iOS 和/或 Android 设置后,您可以简单地调用此方法:
await IconSwitcher.changeIcon('first', 'com.example.example.First');
这里的第一个参数是 IconName,这部分主要对 iOS 是必需的,第二个参数是 Android 的 ApplicationId 对应的 Activity。
笔记
请注意,如前所述,Android 的解决方案不是最佳解决方案,但它确实有效。对我来说,Android 更新主屏幕上的应用程序需要一些时间,因此按下图标并看到“未知应用程序”之类的消息可能会发生!这需要一点时间,然后应用程序就会带新图标回来,并且可以正常启动。