Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
988 views
in Technique[技术] by (71.8m points)

android - ActionBarSherlock items not appearing in overflow

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:

enter image description here

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.

enter image description here

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:

enter image description here

in Landscape:

enter image description here


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

enter image description here

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...