开发者

Android: changing drawable states of option menu items seems to have side-effects

开发者 https://www.devze.com 2023-02-02 16:39 出处:网络
In my onCreateOptionsMenu() I have basically the following: public boolean onCreateOptionsMenu(Menu menu) {

In my onCreateOptionsMenu() I have basically the following:

public boolean onCreateOptionsMenu(Menu menu) {

        menu.add(Menu.NONE, MENU_ITEM_INSERT, Menu.NONE, R.string.item_menu_insert).setShortcut('3',
 开发者_StackOverflow中文版               'a').setIcon(android.R.drawable.ic_menu_add);

        PackageManager pm = getPackageManager();
        if(pm.hasSystemFeature(PackageManager.FEATURE_CAMERA) && pm.hasSystemFeature(PackageManager.FEATURE_CAMERA_AUTOFOCUS)){
            menu.add(Menu.NONE, MENU_ITEM_SCAN_ADD, Menu.NONE, ((Collectionista.DEBUG)?"DEBUG Scan and add item":getString(R.string.item_menu_scan_add))).setShortcut('4',
                    'a').setIcon(android.R.drawable.ic_menu_add);
        }
        ...
}

And in onPrepareOptionsMenu among others the following:

final boolean scanAvailable = ScanIntent.isInstalled(this);
final MusicCDItemScanAddTask task = new MusicCDItemScanAddTask(this);          
menu.findItem(MENU_ITEM_SCAN_ADD).setEnabled(scanAvailable && (tasks == null || !existsTask(task)));

As you see, two options items have the same drawable set (android.R.drawable.ic_menu_add). Now, if in onPrepareOptionsMenu the second menu item gets disabled, its label and icon become gray, but also the icon of the first menu item becomes gray, while the label of that first menu item stays black and it remains clickable. What is causing this crosstalk between the two icons/drawables? Shouldn't the system handle things like mutate() in this case?

I've included a screenshot:

Android: changing drawable states of option menu items seems to have side-effects


http://www.curious-creature.org/2009/05/02/drawable-mutations/

The above article by Romain Guy explains this very situation and provides a work around.


Yes, this looks odd. I can not explain why this is as it is, however I can propose a workaround - instead of using internal drawable resourse, you could put the same image in your app drawable resourse dir AND you could duplicate the add image, so you have 2 images - add_for_menu_item_1.png and add_for_menu_item_2.png named differently, but having the same visual representation. I am sure this would do the trick.


Could it be that both menu items are sharing the same alphaChar which is causing the second menuItem to be disabled?

0

精彩评论

暂无评论...
验证码 换一张
取 消