Android以编程方式更改应用程序图标 [英] Android change app icon programatically

查看:150
本文介绍了Android以编程方式更改应用程序图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到其他android应用有一个应用图标选择,然后更改立即反映在应用抽屉和主屏幕上.

I have seen other android apps have a selection for app icon, and then the changes are reflected in the app drawer and on home screen instantly.

这些应用如何更改这样的应用图标?

How do these apps change app icon like this?

推荐答案

首先为每种分辨率准备新的应用程序图标,然后将它们放入相应的mipmap文件夹中.

First prepare your new app icons for each resolution and put them into the corresponding mipmap folders.

然后使用活动别名,例如,在您的AndroidManifest.xml中,编辑以下内容:

Then use activity-alias, for example, in your AndroidManifest.xml, edit this:

    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>

            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>

    <activity-alias
        android:name=".MainAliasActivity"
        android:enabled="false"
        android:icon="@mipmap/ic_launcher_fight"
        android:label="Main Alias Activity"
        android:targetActivity=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity-alias>

要更改应用程序图标时,添加以下代码

Add the following code when you want to change your app icon

        PackageManager pm = getPackageManager();
        pm.setComponentEnabledSetting(
                getComponentName(),
                PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                PackageManager.DONT_KILL_APP);
        pm.setComponentEnabledSetting(
                new ComponentName(this, "YOUR PACKAGE.MainAliasActivity"),
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                PackageManager.DONT_KILL_APP);

要重置应用程序图标,您可以使用相同的方法.

To reset the app icon you can use the same way.

GitHub项目此处

这篇关于Android以编程方式更改应用程序图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆