选项菜单 通过res\menu下的XML文件可以配置选项菜单。
<menu xmlns:android="http://schems.android.com/apk/res/android" xmls:tools="http://schemas.android.com/tools" tools:context="cn.edu.menu.MainActivity"> <item android:id="@+id/action_setings" android:orderInCategory="100" android:showAsAction="never" android:title="@string/action_settings"/> </menu>
public boolean onCreateOptionMenu(Menu menu){ getMenuInflater().inflate(R.menu.main,menu); return true; }
public boolean onOptionsItemSelected(Menuitem item){ int id = item.getItemId(); if(id==R.id.action_settings){ return true; } return super.onOptionsItemSelected(item); }
<menu xmlns:android="http://schems.android.com/apk/res/android" xmls:tools="http://schemas.android.com/tools" tools:context="cn.edu.menu.MainActivity"> <item android:id="@+id/action_file" android:orderInCategory="101"//菜单项的排列顺序 android:showAsAction="never" android:title="file"/> <item android:id="@+id/action_refactor" android:orderInCategory="103" android:showAsAction="never" android:title="Refactor"/> <item android:id="@+id/action_edit" android:orderInCategory="102" android:showAsAction="never" android:title="Edit"/> </menu> Activity: public boolean onOptionsItemSelected(Menuitem item){ int id = item.getItemId(); switch(id){ case R.id.action_file: Toast.makeText(this,"您选择了File",Toast。LENGTH_SHORT).show(); break; case R.id.action_edit: Toast.makeText(this,"您选择了Edit",Toast。LENGTH_SHORT).show(); break; case R.id.action_refactor: Toast.makeText(this,"您选择了Refactor",Toast。LENGTH_SHORT).show(); break; } return super.onOptionsItemSelected(item); }
public MenuItem add(int groupId,int itemId,int order,CharSequence title) public MenuItem add(int groupId,int itemId,int order,int titleRes)
private static final int MENU_FILE_ID = 9527; private static final int MENU_EDIT_ID = 1111; private static final int MENU_REFACTOR_ID = 2222; private static final int MENU_WINDOW_ID = 3333; private static final int MENU_HELP_ID = 4444; public boolean onCreateOptionsMenu(Menu menu){ menu.add(Menu.NONE,MENU_FILE_ID,1,"File"); menu.add(Menu.NONE,MENU_HELP_ID,5,"Help"); menu.add(Menu.NONE,MENU_WINDOW_ID,4,"Window"); menu.add(Menu.NONE,MENU_REFACTOR_ID,3,"Refactor"); menu.add(Menu.NONE,MENU_EDIT_ID,2,"Edit"); return true; } public boolean onOptionsItemSelected(Menuitem item){ int id = item.getItemId(); switch(id){ case MENU_FILE_ID: Toast.makeText(this,"您选择了File",Toast。LENGTH_SHORT).show(); break; case MENU_EDIT_ID: Toast.makeText(this,"您选择了Edit",Toast。LENGTH_SHORT).show(); break; case MENU_REFACTOR_ID: Toast.makeText(this,"您选择了Refactor",Toast。LENGTH_SHORT).show(); break; case MENU_WINDOW_ID: Toast.makeText(this,"您选择了Window",Toast。LENGTH_SHORT).show(); break; case MENU_HELP_ID: Toast.makeText(this,"您选择了Help",Toast。LENGTH_SHORT).show(); break; } return super.onOptionsItemSelected(item); }
private static final int MENU_FILE_ID = 9527; private static final int MENU_EDIT_ID = 1111; private static final int MENU_REFACTOR_ID = 2222; private static final int MENU_WINDOW_ID = 3333; private static final int MENU_HELP_ID = 4444; private static final int MENU_SOURCE_ID = 8888; public boolean onCreateOptionsMenu(Menu menu){ menu.add(Menu.NONE,MENU_FILE_ID,1,"File"); menu.add(Menu.NONE,MENU_HELP_ID,5,"Help"); menu.add(Menu.NONE,MENU_WINDOW_ID,4,"Window"); menu.add(Menu.NONE,MENU_REFACTOR_ID,3,"Refactor"); menu.add(Menu.NONE,MENU_EDIT_ID,2,"Edit"); SubMenu subMenu = menu.addSubMenu(Menu.NONE,MENU_SOURCE_ID,4,"source"); subMenu.add(Menu.NONE,0,1,"Format"); subMenu.add(Menu.NONE,1,2,"Clean Up"); return true; }
public boolean onCreateOptionMenu(Menu menu){ getMenuInflater().inflate(R.menu.main,menu); return true; } <menu xmlns:android="http://schems.android.com/apk/res/android" xmls:tools="http://schemas.android.com/tools" tools:context="cn.edu.menu.MainActivity"> <item android:id="@+id/action_file" android:orderInCategory="101"//菜单项的排列顺序 android:showAsAction="never" android:title="file"> <menu> <item android:id="@+id/action_file_open" android:orderInCategory="1022" android:showAsAction="never" android:title="Open"/> <item android:id="@+id/action_file_save" android:orderInCategory="1012" android:showAsAction="never" android:title="Save"/> </item> <item android:id="@+id/action_refactor" android:orderInCategory="103" android:showAsAction="never" android:title="Refactor"/> <item android:id="@+id/action_edit" android:orderInCategory="102" android:showAsAction="never" android:title="Edit"/> </menu>
private ImageView imageview; registerForContextMenu(imageview); private static final int MENU_CHANGE_ID = 9521; private static final int MENU_SAVE_ID =9522; public void onCreateContextMenu(ContextMenu menu,View v, ContextMenuInfo menuInfo){ menu.add(Menu.NONE,MENU_CHANGE_ID,1,"Change"); menu.add(Menu.NONE,MENU_SAVE_ID,2,"Save"); super.onCreateContextMenu(menu,v,menuInfo); } public boolean onContextItemSelected(MenuItem item){ switch(item.getItemId()){ case MENU_CHANGE_ID: break; case MENU_SAVE_ID: break; } return super.onContextItemSelected(item); }