开发者

Submenu within menu within menu?

On pressing menu button , I have 2 options : Add & more. On click of more i have 3 options : Organize ,Export & Exit On click of Organize i want other 5 options.

On click of more i get my submenu. But i want other 5 options on click of organize.How do i proceed???

My code in parts is as follows : XML file-------------------------------

<?xml version="1.0" encoding="utf-8"?>   
  <menu xmlns:android="http://schemas.android.com/apk/res/android">

   <item    
    android:id="@+id/more"    
    android:title="@string/moreMenu"    
    android:icon="@drawable/icon">
    <menu>
        <item android:id="@+id/Organize"
        android:title="@string/Organize" />

        <item android:id="@+id/Export"
        android:title="@string/Export" />
    </menu>  
   </item> 

   <item    
    android:id="@+id/add"    
    android:title="@string/addMenu"  
    android:icon="@drawable/add"/>
   </menu>

Java-------------------------

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class ToDoList extends Activity {
 Menu menu;
  public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.todolist); 

   }



  public boolean onCreateOptionsMenu(Menu menu) {   
         super.onCreateOptionsMenu(menu);   
         getMenuInflater().inflate(R.layout.categorymenu, menu);

         return true; 


     }  
   public boolean onOptionsItemSelected(MenuIt开发者_如何学Cem item) {   
         switch (item.getItemId()) { 

         case R.id.more:   
               Toast.makeText(this, "You pressed more!", Toast.LENGTH_LONG).show();
//(What needs to be done from here)
                    return true;

         case R.id.add:
           Toast.makeText(this, "You pressed add!", Toast.LENGTH_LONG).show();
          return true;
           }
         return false;
         }
   public boolean onPrepareOptionsMenu(Menu menu) {   
                   return true;   
     }
  }


As can be seen in Creating Menus sub menus can not contain sub menus.

You could show a Context menu after clicking on the item in the options menu. This could show your five more options in a floating view above the screen.

You have to overwrite the onCreateContextMenu to create a ContextMenu and I think you have to call the contextMenu manually in the onOptionsItemSelected method. For resources on how to create the context menu see this paragraph in the article mentioned above.

To open the ContextMenu you can call openContextMenu in your Activity. You may need to register the menuitem before to enable your activity to find the correct context menu.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜