用xml配置文件:
//创建菜单 public boolean onCreatOptionsMenu(Menu menu){ MenuInflater mm =getMenuInflater(); mm.inflate(R.menu.n, menu); return false; } //处理菜单事件 public boolean onOptionsItemSelected(MenuItem item){ int id = item.getItemId(); switch(id){ case R.id.item1: Toast.makeText(this, "触发了菜单", 5000).show(); break; case R.id.item2: break; } return false; }
通过代码创建:
public boolean onCreatOptionsMenu(Menu menu){ menu.add(0,0,0,"菜单1"); menu.add(0,1,1,"菜单2"); return false; }