简体   繁体   English

如何从操作栏项创建下拉选项菜单?

[英]How can I create a dropdown options menu from my action bar item?

I am using actionbarsherlock and have set up an item in my action bar. 我正在使用actionbarsherlock,并在我的操作栏中设置了一个项目。 Now I would like that on click on that item, a dropdown menu appears that shows two more options. 现在,我希望在单击该项目时出现一个下拉菜单,其中显示了另外两个选项。 What should I do? 我该怎么办? This is my code so far: 到目前为止,这是我的代码:

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


@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()) {
    // ?????
    // ?????

    }
}

Per the Defining Menus via XML Guide : 根据“ 通过XML定义菜单指南”

You can add a submenu to an item in any menu (except a submenu) by adding a <menu> element as the child of an <item> . 您可以通过将<menu>元素添加为<item>的子元素,将子<menu>添加到任何菜单中的项目(子菜单除外)。 Submenus are useful when your application has a lot of functions that can be organized into topics, like items in a PC application's menu bar (File, Edit, View, etc.). 当您的应用程序具有许多可以组织为主题的功能时,例如PC应用程序菜单栏中的项目(文件,编辑,视图等),子菜单将非常有用。

They give an example XML of: 他们给出了一个XML示例:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/file"
          android:title="@string/file" >
        <!-- "file" submenu -->
        <menu>
            <item android:id="@+id/create_new"
                  android:title="@string/create_new" />
            <item android:id="@+id/open"
                  android:title="@string/open" />
        </menu>
    </item>
</menu>

In this case, your onOptionsItemSelected would look for create_new and open actions (and the file item would be handled by the menu itself). 在这种情况下,您的onOptionsItemSelected将查找create_newopen动作( file项将由菜单本身处理)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM