1,子菜单就是将功能相同或相似的分组进行多级显示的一种菜单。
2,创建子菜单的步骤:
a) 覆盖Activity的onCreateOptionsMenu()方法,调用Menu的addSubMenu()方法来添加子菜单
b) 调用SubMenu的add()方法,添加子菜单
c) 覆盖onContextItemSelected()方法,响应子菜单的单击事件
3,使用代码动态添加SubMenu子菜单:
package com.example.submenu;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/*
*设置menu子菜单显示的内容
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
//getMenuInflater().inflate(R.menu.main, menu);
//通过addSubMenu方法添加两个子菜单
SubMenu file=menu.addSubMenu("文件");
SubMenu edit=menu.addSubMenu("编辑");
//设置file子菜单中的内容(设置GroupId为1)
file.add(1, 1, 1, "新建");
file.add(1, 2, 1, "打开");
file.add(1, 2, 1, "保存");
file.setHeaderTitle("文件操作"); //设置子菜单的标题
file.setHeaderIcon(R.drawable.ic_launcher); //设置标题旁的图片
//设置edit子菜单中的内容(设置GroupId为2)
edit.add(2, 1, 1, "复制");
edit.add(2, 2, 1, "粘贴");
edit.add(2, 3, 1, "剪切");
edit.setHeaderTitle("编辑操作");
edit.setHeaderIcon(R.drawable.ic_launcher);
return true;
}
/*
* 设置菜单项的点击事件
*/
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// 通过GroupId来判断是哪一个子菜单
if (item.getGroupId()==1) {
//通过ItemId来判断选中的是子菜单中的哪一项
switch (item.getItemId()) {
case 1:
Toast.makeText(this, "点击了新建", Toast.LENGTH_SHORT).show();
break;
case 2:
Toast.makeText(this, "点击了打开", Toast.LENGTH_SHORT).show();
break;
case 3:
Toast.makeText(this, "点击了保存", Toast.LENGTH_SHORT).show();
break;
}
}else if(item.getGroupId()==2){
switch (item.getItemId()) {
case 1:
Toast.makeText(this, "点击了复制", Toast.LENGTH_SHORT).show();
break;
case 2:
Toast.makeText(this, "点击了粘贴", Toast.LENGTH_SHORT).show();
break;
case 3:
Toast.makeText(this, "点击了剪切", Toast.LENGTH_SHORT).show();
break;
}
}
return super.onOptionsItemSelected(item);
}
}
4,使用XML添加SubMenu子菜单(在res的menu中编写代码):
5,使用XML添加SubMenu子菜单时MainActivity中的代码:
package com.example.submenu;
import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.Toast;
public class MainActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
//super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.main, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.new_file:
Toast.makeText(this, "点击了新建", Toast.LENGTH_SHORT).show();
break;
case R.id.open_file:
Toast.makeText(this, "点击了打开", Toast.LENGTH_SHORT).show();
break;
case R.id.save_file:
Toast.makeText(this, "点击了保存", Toast.LENGTH_SHORT).show();
break;
case R.id.c_edit:
Toast.makeText(this, "点击了复制", Toast.LENGTH_SHORT).show();
break;
case R.id.v_edit:
Toast.makeText(this, "点击了粘贴", Toast.LENGTH_SHORT).show();
break;
case R.id.x_edit:
Toast.makeText(this, "点击了剪切", Toast.LENGTH_SHORT).show();
break;
}
return super.onOptionsItemSelected(item);
}
}
6,实现效果:
点击子菜单项后的效果,例如点击“文件”: