首页 > 解决方案 > 如何以编程方式增加Android中快捷图标的大小

问题描述

在我的应用程序中,我使用下面的代码来创建快捷方式。

 public void createShortcut(Intent intent) {
    Intent.ShortcutIconResource iconResource = intent.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
    Bitmap icon = intent.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
    String shortcutLabel = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
    Intent shortIntent = intent.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);

    if (icon == null) {
        if (iconResource != null) {
            Resources resources = null;
            try {
                resources = pm.getResourcesForApplication(iconResource.packageName);
            } catch (NameNotFoundException e) {
                e.printStackTrace();
            }
            if (resources != null) {
                int id = resources.getIdentifier(iconResource.resourceName, null, null);
                if (resources.getDrawable(id) instanceof StateListDrawable) {
                    Drawable d = (resources.getDrawable(id)).getCurrent();
                    icon = ((BitmapDrawable) d).getBitmap();
                } else
                    icon = ((BitmapDrawable) resources.getDrawable(id)).getBitmap();
            }
        }
    }

    if (shortcutLabel != null && shortIntent != null && icon != null) {
        ShortcutSerializableData objectData = AppSharedPref.getInstance().getShortcutSerializableData();
        if (objectData == null) {
            objectData = new ShortcutSerializableData();
            Log.e(TAG, "Serialized data not found");
        }

        if (objectData.apps == null) {
            objectData.apps = new ArrayList();
            Log.e(TAG, "Serialized app not found");
        }


        ShortcutPac pacToAdd = new ShortcutPac();

        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        int width = size.x;
        int height = size.y;
        if (x >= (width - 250)) {
            y = y + 150;
            x = 40;
        }


        pacToAdd.x = x;
        pacToAdd.y = y;
        pacToAdd.URI = shortIntent.toUri(0);
        pacToAdd.label = shortcutLabel;
        pacToAdd.icon = icon;
        x = x + 250;

        if (HomeActivity.activity.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
            pacToAdd.landscape = true;
        else
            pacToAdd.landscape = false;

        pacToAdd.cacheIcon();
        objectData.apps.add(pacToAdd);
        pacToAdd.addToHome(this, homeView);
        AppSharedPref.getInstance().setShortcutSerializableData(objectData);
    }
}

上面的代码正在按预期工作并创建捷径。我的问题是可以改变快捷图标的大小吗?我的意思是通过上面的代码,我得到了图标的大小(例如 50x50),然后我得到了与 7 英寸和 10 英寸平板电脑上的快捷图标相同的大小。我想要的是更改平板电脑快捷图标的大小。我想显示比平常更大的平板电脑快捷方式图标,因为它是为手机创建的。

注意:在 Android 操作系统的设置中,如果我选择显示选项,我可以从那里增加图标和文本的大小。但是我想要同样的事情,我想以编程方式更改快捷图标的大小。可能吗?任何帮助将不胜感激。

标签: android

解决方案


图标显示大小取决于启动器。幸运的是,您无法强制执行用户想要的不同大小。


推荐阅读