Android学习历程-子菜单(Sub Menu)
package com.example.testcontextmenu; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.SubMenu; public class TestSubMenu extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { SubMenu sub = menu.addSubMenu("文件"); SubMenu edit = menu.addSubMenu("编辑"); sub.add(0, Menu.FIRST, 0, "新建"); sub.add(0, Menu.FIRST + 1, 0, "打开"); edit.add(0, Menu.FIRST + 2, 0,"剪切"); edit.add(0, Menu.FIRST + 3, 0,"复制"); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case Menu.FIRST: setTitle("新建文件"); break; case (Menu.FIRST+1): setTitle("打開文件"); break; case (Menu.FIRST+2): setTitle("剪切文件"); break; case (Menu.FIRST+3): setTitle("复制文件"); break; } return true; } }