首页 > 解决方案 > 如何以编程方式生成图标并使用它而不是快捷方式图标?

问题描述

有没有办法以编程方式生成图标(恒定背景和中心文本(来自可以更改的变量))并使用它而不是快捷方式图标?

标签: javaandroidiconsandroid-drawableshortcut

解决方案


看起来 Android 的 ShortcutManager 可能最适合您的需求,因为它允许静态和动态生成快捷方式。 https://developer.android.com/guide/topics/ui/shortcuts.html#dynamic

ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);

ShortcutInfo shortcut = new ShortcutInfo.Builder(this, SHORTCUT_ID)
    .setShortLabel("My Shortcut")
    .setLongLabel("This is my shortcut")
    .setIcon(Icon.createWithResource(context, R.drawable.mipmap))
    .setIntent(SHORTCUT_ACTION)
    .build();

shortcutManager.setDynamicShortcuts(Arrays.asList(shortcut));

推荐阅读