ActionBarSherlock items not appearing in overflow

2019-01-20 10:36发布

问题:

I'm trying to restyle my app to an ICS look and feel with ABS. Getting the ActionBar itself was nice and straightforward, however adding menu items has not been.

My code looks like:

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        MenuInflater inflater = getSupportMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }

All the appropriate imports are being used.

And my menu.xml is as follows:

<?xml version="1.0" encoding="utf-8"?>
<menu
  xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:id="@+id/backup" android:title="@string/backupLabel"/>
  <item android:id="@+id/restore" android:title="@string/restoreLabel"/>
</menu>

The ActionBar shows, but the menu behaves as a 2.1 menu - only activating from the menu button with no overflow available. This is also true on an ICS emulator - where I have to use the menu button to activate the menu.

If I add android:showAsAction="ifRoom" then the items appear as action items - but not in the overflow, which is where I would like them to always be.

Does that all make sense?

What am I missing?

回答1:

just use this code boath menu are on ActionBar:

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

    <item
        android:id="@+id/backup"
        android:showAsAction="ifRoom"
        android:title="backupLabel"/>
    <item
        android:id="@+id/restore"
        android:showAsAction="ifRoom"
        android:title="restoreLabel"/>
</menu> 

its look like:

Because if you are use

android:showAsAction="ifRoom" : Only place this item in the Action Bar if there is room for it.

and

android:showAsAction="Never" : Never place this item in the Action Bar.

if you use like :

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

        <item
            android:id="@+id/backup"
            android:showAsAction="ifRoom"
            android:title="backupLabel"/>
        <item
            android:id="@+id/restore"
            android:title="restoreLabel"/>
    </menu> 

then its look: one is on ActionBar & 2nd is open when you press menu button.

Note : Its working fine in < 3.0 Android OS so don't worry. But if you have multiple menu item(more then 3) with android:showAsAction="ifRoom" then it show you depend on device screen size.

in Protrait:

in Landscape:


After Doing all above i ll saggest you to use below way in < 3.0 Android os:

code:

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

    <item
        android:id="@+id/file"
        android:showAsAction="ifRoom"
        android:title="file">

        <!-- "file" submenu -->
        <menu>
            <item
                android:id="@+id/create_new"
                android:title="create_new"/>
            <item
                android:id="@+id/open"
                android:title="open"/>
        </menu>
    </item>

</menu>


回答2:

ForceOverflow has been removed in ABS 4.1.2 with the creator sighting that it should never have made it in (see the changelog notes). I am pretty confident you are seeing expected behaviour; any device with a menu button running ICS+ (e.g. my Nexus S) does not display the overflow menu since there is a dedicated hardware key - the 'menu' button.

Use the emulator configuration to remove the menu button from the emulated device and Android will know to add the overflow menu.



回答3:

You are likely using an emulator that has a hardware keyboard enabled, and not software. To see actions in the overflow menu, use an emulator with sofwater controls. The screeenshots show two ways to do it.

  • Edit an existing AVD

.

  • Clone an existing device profile

.