openOptionsMenu function not working in ICS?

后端 未结 8 1963
一整个雨季
一整个雨季 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:04

    To shed some light on this sad development by google. Google obviously wishes everybody to embrace the new ActionBar. They could have achieved that by making the ActionBar easier to use than the old menu system. That is however not how they planned the transition. No, they thought it would make sense to harras programmers by making the old menus impossible to use but without providing proper backward compatibility.

    Below is the code taken from com.android.internal.policy.impl, which is supposed to create the optionsMenu panel. As you see, the code simply refuses to create an options Panel. Allthough, the ability is obviously there. So, to answer your question: forget it, Google doesn't want you to use that optionsPanel anymore.

     // Don't open an options panel for honeycomb apps on xlarge devices.
     // (The app should be using an action bar for menu items.)
     if (st.featureId == FEATURE_OPTIONS_PANEL) {
                Context context = getContext();
                Configuration config = context.getResources().getConfiguration();
                boolean isXLarge = (config.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) ==
                        Configuration.SCREENLAYOUT_SIZE_XLARGE;
                boolean isHoneycombApp = context.getApplicationInfo().targetSdkVersion >=
                        android.os.Build.VERSION_CODES.HONEYCOMB;
    
                if (isXLarge && isHoneycombApp) {
                    return;
                }
            }
    
    0 讨论(0)
  • 2020-12-03 22:05

    Do you mean you want to show a button on the right side of the action bar?

    Here is how I did mine:

    res/menu/main.xml

    <menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/menu_share"
        android:title="Logout"
        android:icon="@android:drawable/ic_lock_power_off"
        android:orderInCategory="1"
        android:showAsAction="always" />
    </menu>
    

    Activity
    1) take note of the ActionBarActivity; 2) MenuInflater in onCreateOptionsMenu 3) onOptionsItemsSelected (I think you need to return super.onOptionsItemSelected(item) )

    public class BaseActivity extends ActionBarActivity {
        ....
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
    
        return true;
    }
    ....
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    
        switch (item.getItemId()) {
        case R.id.menu_share:
    
    
           //Do something
            break;
        }
    
        return super.onOptionsItemSelected(item);
    }
    
    0 讨论(0)
提交回复
热议问题