如何在操作栏右上角移除设置图标(溢出菜单)?

27

如何从操作栏的右上角删除设置图标?当我在实际的三星Galaxy s3手机上模拟时,它不存在,但是在Nexus 7的AVD上模拟时,则会出现。这是我的菜单代码:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="@string/action_settings"/>
    <item
        android:id="@+id/main_menu_actionbutton"
        android:orderInCategory="100"
        android:showAsAction="always"
        android:title="@string/Menu"/>
</menu>

1
好的,只需删除那个项目。 - Ivan Skoric
3个回答

54
如果您指的是三个点,那被称为溢出菜单。它存在于没有硬件菜单按钮的设备上 - 您的Galaxy S3有该按钮,而Nexus 7没有。因此,在S3上,您按下菜单按钮并获得底部弹出窗口,但在Nexus上,您按下3个点并从操作栏中获得溢出弹出窗口。如果不存在,您将如何访问溢出项?
如果您只想彻底删除它,请在您发布的menu.xml中删除第一个<item />条目即可。

有没有示例,请帮帮我。 - vijay

30

您的 "MainActivity" Java 类中有两种方法。它们被称为 onCreateOptionsMenu 和 onOptionsItemSelected。这些通常是像 Eclipse 这样的 IDE 在使用默认设置创建项目活动时添加的。方法看起来像下面显示的那样。

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.settings, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

这些方法创建菜单项并插入代码,以指定当其中一个菜单项被点击时需要执行的操作。只需删除或注释掉这两种方法中的任何一种,您就可以看到菜单按钮会消失。 XMLs 可以保留它们,以防您想要恢复您的菜单项或菜单。


3

如果android:showAsAction属性被设置为never,该项将成为溢出菜单的一部分。将其设置为always或ifroom,或者删除此项即可解决问题。

<item
    android:id="@+id/action_settings"
    android:orderInCategory="100"
    android:showAsAction="never"
    android:title="@string/action_settings"/>

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