package com.test.menu; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.SubMenu; import android.widget.Toast; public class MenuDemoActivity extends Activity { private static final int ITEM1 = Menu.FIRST; private static final int ITEM2 = Menu.FIRST + 1; private static final int ITEM3 = Menu.FIRST + 2; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public boolean onCreateOptionsMenu(Menu menu) { SubMenu file = menu.addSubMenu("文件"); // file.setHeaderIcon(icon); file.setHeaderTitle("请选择文件操作"); SubMenu edit = menu.addSubMenu("编辑"); file.setIcon(getResources().getDrawable(R.drawable.ic_launcher)); file.add(0, ITEM1, 0, "新建"); file.add(0, ITEM2, 0, "打开"); edit.setIcon(getResources().getDrawable(R.drawable.ic_launcher)); MenuItem setting= menu.add(0,0x123, 0, "设置"); setting.setIcon(getResources().getDrawable(R.drawable.ic_launcher)); return super.onCreateOptionsMenu(menu); } public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case ITEM1: setTitle("新建文件"); Toast.makeText(this, "create new", 0).show(); break; case ITEM2: setTitle("打开文件"); Toast.makeText(this, "open file", 0).show(); break; case 0x123: setTitle("设置中心"); Toast.makeText(this, "进入设置", 0).show(); break; } return super.onOptionsItemSelected(item); } }
组菜单
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/item1" android:icon="@drawable/ic_launcher" android:title="菜单一"> <menu > <!-- menu group --> <group android:id="@+id/group1" android:checkableBehavior="single" > <item android:id="@+id/groupItem1" android:title="组一"/> <item android:id="@+id/groupItem2" android:title="组二"/> <item android:id="@+id/groupItem3" android:title="组3"/> <item android:id="@+id/groupItem4" android:title="组4"/> <item android:id="@+id/groupItem5" android:title="组5"/> <item android:id="@+id/groupItem6" android:title="组6"/> <item android:id="@+id/groupItem7" android:title="组7"/> <item android:id="@+id/groupItem8" android:checked="true" android:title="组8"/> </group> </menu></item> <item android:id="@+id/item2" android:title="菜单二"> </item> </menu>
package com.test.menu; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.SubMenu; import android.widget.Toast; public class MenuDemoActivity extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.option, menu); return true; } public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.groupItem1: case R.id.groupItem2: case R.id.groupItem3: case R.id.groupItem4: case R.id.groupItem5: case R.id.groupItem6: case R.id.groupItem7: case R.id.groupItem8: if (item.isChecked()) item.setChecked(false); else item.setChecked(true); return true; default: return super.onOptionsItemSelected(item); } } }