ActionBarSherlock的项目未出现在溢出中

6

我正在尝试使用ABS将我的应用重新设计成ICS风格,使其外观和感觉更加现代化。获取ActionBar本身很容易,但是添加菜单项却不是那么简单。

我的代码如下:

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

所有必要的导入已经被使用。

我的menu.xml文件如下:

<?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>

ActionBar显示出来了,但是菜单的表现却像一个2.1版本的菜单——只能通过菜单按钮激活,没有溢出区域可用。在一个ICS模拟器上也是如此,我必须使用菜单按钮来激活菜单。
如果我添加android:showAsAction="ifRoom",那么这些项就会变成操作项——但不会出现在溢出区域中,我希望它们始终在那里。
这一切都有意义吗?
我漏掉了什么?

https://dev59.com/M2ox5IYBdhLWcg3wTytx - Dheeresh Singh
https://dev59.com/UWoy5IYBdhLWcg3wHqiB - Dheeresh Singh
保罗,你的问题通过提供的链接解决了吗? - Warpzit
3个回答

4

只需使用此代码,即可使两个菜单都显示在 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> 

样子如下:

在此输入图片描述

因为如果你使用

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

并且

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

如果您使用类似于以下的内容:
<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> 

然后它看起来是这样的:一个在ActionBar上,第二个是按下菜单按钮后打开的。

enter image description here

注意:在< 3.0版本的Android操作系统中工作正常,所以不用担心。但是,如果您有多个具有 android:showAsAction="ifRoom" 属性的菜单项(超过3个),则显示取决于设备屏幕大小

竖屏时:

enter image description here

横屏时:

enter image description here


在执行以上所有步骤后,我建议您在< 3.0 Android OS中使用以下方法:

enter image description here

代码如下:

<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>

1

ForceOverflow在ABS 4.1.2中已被移除,创作者表示它本不应该存在(请参阅更改日志注释)。我非常有信心您正在看到预期的行为;任何具有菜单按钮且运行ICS+(例如我的Nexus S)的设备都不会显示溢出菜单,因为有一个专用硬件键-“菜单”按钮。

使用模拟器配置从模拟设备中删除菜单按钮,Android将知道添加溢出菜单。


0

你可能正在使用启用了硬件键盘而非软件键盘的模拟器。要在溢出菜单中看到操作,请使用带有软件控件的模拟器。下面的截图展示了两种方法。

  • 编辑现有的 AVD

Edit an existing AVD.

  • 克隆现有的设备配置文件

Clone an existing device profile .


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接