Pregunta

I am using actionbarsherlock and have set up an item in my action bar. 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()) {
    // ?????
    // ?????

    }
}
¿Fue útil?

Solución

Per the Defining Menus via XML Guide:

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

They give an example XML of:

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

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top