首页 > 代码库 > Android攻城狮使用SubMenu创建子菜单

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

给OptionsMenu选项菜单添加子菜单:

  1 public class MainActivity extends Activity {
  2 
  3     @Override
  4     protected void onCreate(Bundle savedInstanceState) {
  5         super.onCreate(savedInstanceState);
  6         setContentView(R.layout.fragment_main);
  7 
  8     }
  9 
 10     @Override
 11     public 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文件加载
 17         MenuInflater inflater = getMenuInflater();
 18         inflater.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 
 36         return true;
 37     }
 38 
 39     @Override
 40     public boolean onOptionsItemSelected(MenuItem item) {
 41         // TODO Auto-generated method stub
 42         // xml加载:
 43         switch (item.getItemId()) {
 44         case R.id.new_file:
 45             Toast.makeText(MainActivity.this, "点击新建", 0).show();
 46 
 47             break;
 48         case R.id.open_file:
 49             Toast.makeText(MainActivity.this, "点击打开", 0).show();
 50 
 51             break;
 52         case R.id.save_file:
 53             Toast.makeText(MainActivity.this, "点击保存", 0).show();
 54 
 55             break;
 56         case R.id.c_edit:
 57             Toast.makeText(MainActivity.this, "点击复制", 0).show();
 58 
 59             break;
 60         case R.id.v_edit:
 61             Toast.makeText(MainActivity.this, "点击粘贴", 0).show();
 62 
 63             break;
 64         case R.id.x_edit:
 65             Toast.makeText(MainActivity.this, "点击剪切", 0).show();
 66 
 67             break;
 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         // }
100         return super.onOptionsItemSelected(item);
101     }
102 }

技术分享技术分享

 

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