openOptionsMenu function not working in ICS?

后端 未结 8 2009
一整个雨季
一整个雨季 2020-12-03 21:07

Im using action bar compability library. Im trying to open the options menu from a button with openOptionsMenu() function but it does nothing.

Menu shows as usual wh

8条回答
  •  抹茶落季
    2020-12-03 22:01

    I cannot understand the forcing blocking Menu button usage as well. However, the following trick helped me with showing a menu on "restricted" types of devices.

    First of all we need to define, whether we require the following hack or not.

    boolean requireDirtyMenuButtonHack = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB && (activity.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_XLARGE) > 0;
    

    Then:

    protected final OnClickListener mMenuButtonClickListener = new OnClickListener() {
    
        @Override
        public void onClick(View v) {
            if (requireDirtyMenuButtonHack) {
                Configuration config = getContext().getResources().getConfiguration();
                config.screenLayout &= ~Configuration.SCREENLAYOUT_SIZE_XLARGE;
                config.screenLayout |= Configuration.SCREENLAYOUT_SIZE_LARGE;
            }
    
            getActivity().openOptionsMenu();
        }
    };
    

    Don't forget to cleanup! (don't know if it's neccessary, but better to play kind)

    public void onPrepareOptionsMenu(Menu menu) {
        if (requireDirtyMenuButtonHack) {
            Configuration config = getContext().getResources().getConfiguration();
            config.screenLayout &= ~Configuration.SCREENLAYOUT_SIZE_LARGE;
            config.screenLayout |= Configuration.SCREENLAYOUT_SIZE_XLARGE;
        }
    
        //do the preparing...
    }
    

提交回复
热议问题