Android攻城狮使用SubMenu创建子菜单

知识就是力量,时间就是生命。这篇文章主要讲述Android攻城狮使用SubMenu创建子菜单相关的知识,希望能为你提供帮助。
给OptionsMenu选项菜单添加子菜单:

1 public class MainActivity extends Activity { 2 3@Override 4protected void onCreate(Bundle savedInstanceState) { 5super.onCreate(savedInstanceState); 6setContentView(R.layout.fragment_main); 7 8} 9 10@Override 11public boolean onCreateOptionsMenu(Menu menu) { 12 13// Inflate the menu; this adds items to the action bar if it is present. 14// getMenuInflater().inflate(R.menu.main, menu); 15 16// xml文件加载 17MenuInflater inflater = getMenuInflater(); 18inflater.inflate(R.menu.main, menu); 19 20// 代码动态添加子菜单 21// SubMenu file = menu.addSubMenu("文件"); 22// SubMenu edit = menu.addSubMenu("编辑"); 23// 24// file.add(1, 1, 1, "新建"); 25// file.add(1, 2, 1, "打开"); 26// file.add(1, 3, 1, "保存"); 27// file.setHeaderTitle("文件操作"); //子菜单标题 28// file.setHeaderIcon(R.drawable.ic_launcher); //子菜单图标 29// 30// edit.add(2, 1, 1, "复制"); 31// edit.add(2, 2, 1, "粘贴"); 32// edit.add(2, 3, 1, "剪切"); 33// file.setHeaderTitle("编辑操作"); 34// file.setHeaderIcon(R.drawable.ic_launcher); 35 36return true; 37} 38 39@Override 40public boolean onOptionsItemSelected(MenuItem item) { 41// TODO Auto-generated method stub 42// xml加载: 43switch (item.getItemId()) { 44case R.id.new_file: 45Toast.makeText(MainActivity.this, "点击新建", 0).show(); 46 47break; 48case R.id.open_file: 49Toast.makeText(MainActivity.this, "点击打开", 0).show(); 50 51break; 52case R.id.save_file: 53Toast.makeText(MainActivity.this, "点击保存", 0).show(); 54 55break; 56case R.id.c_edit: 57Toast.makeText(MainActivity.this, "点击复制", 0).show(); 58 59break; 60case R.id.v_edit: 61Toast.makeText(MainActivity.this, "点击粘贴", 0).show(); 62 63break; 64case R.id.x_edit: 65Toast.makeText(MainActivity.this, "点击剪切", 0).show(); 66 67break; 68} 69// 代码加载: 70// if (item.getGroupId() == 1) { 71// switch (item.getItemId()) { 72// case 1: 73// Toast.makeText(MainActivity.this, "点击新建", 0).show(); 74// break; 75// 76// case 2: 77// Toast.makeText(MainActivity.this, "点击打开", 0).show(); 78// break; 79// case 3: 80// Toast.makeText(MainActivity.this, "点击保存", 0).show(); 81// break; 82// } 83// } 84// 85// if (item.getGroupId() == 2) { 86// switch (item.getItemId()) { 87// case 1: 88// Toast.makeText(MainActivity.this, "点击复制", 0).show(); 89// break; 90// 91// case 2: 92// Toast.makeText(MainActivity.this, "点击粘贴", 0).show(); 93// break; 94// case 3: 95// Toast.makeText(MainActivity.this, "点击剪切", 0).show(); 96// break; 97// 98// } 99// } 100return super.onOptionsItemSelected(item); 101} 102 }

Android攻城狮使用SubMenu创建子菜单

文章图片
Android攻城狮使用SubMenu创建子菜单

文章图片

【Android攻城狮使用SubMenu创建子菜单】 

    推荐阅读