Android ActionbarSherlock show menu button at top

2019-05-23 21:32发布

问题:

I am trying to implement ActionBarSherlock in my application and I need to show the menu at the top of my application, integrated in action bar. The problem is that it's not working properly. I am using it like this :

public boolean onCreateOptionsMenu(Menu menu) {
    menu.add("Refresh")
        .setIcon(R.drawable.ic_action_refresh)
        .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);

    menu.add("Settings")
        .setIcon(R.drawable.ic_action_settings)
        .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
    return true;
}

So the thing which I want to achieve is to show at the right side of my action bar a refresh icon and a menu icon, which should open the default menus when clicked. I checked ForcedOverflowItem example in ActionbarSherlock Demos, but it's not working as I want. I need to look the same as in Android 2.+ and in Android 4.+.

Any advices / helps / suggestions how can I get this to work?

回答1:

From what I read in ActionbarSherlock's documentation you cannot force the menu icon to appear in Android 4.+. When the device has a menu button the menu icon does not appear. I guess the guy who wrote ActionbarSherlock knows the subject matter well ;-)



回答2:

You can do an xml file line this. I saw this in some other post but I cannot track it rigth now.

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/more"
        android:showAsAction="always"
        android:icon="@drawable/abs__ic_menu_moreoverflow_holo_dark">
        <menu>
            <item
                android:id="@+id/remove"
                android:showAsAction="withText|never"
                android:title="@string/remove">
            </item>
        </menu>
    </item>

</menu>

Then just inflate it like a normal menu.

public boolean onCreateOptionsMenu(Menu menu) {
        getSupportMenuInflater().inflate(R.menu.overlow, menu);
        return true;
    }


回答3:

Please read the dorjeduck's answer. If you want have same experience on all devices you have to add custom menu with his submenus. Here this the code sumple:

SubMenu sub = menu.addSubMenu("More");
sub.setIcon(R.drawable.abs__ic_menu_moreoverflow_holo_dark);
sub.add(0, MENU_SETTINGS, 0, "Settings");