Menu 和 Submenu
public boolean onCreateOptionsMenu(Menu menu) {
// getMenuInflater().inflate(R.menu.activity_main, menu);
menu.add(0, 0, 0, R.string.about);
menu.add(0, 1, 1, R.string.ok);
menu.add(0, 2, 2, "苹果");
SubMenu subMenu = menu.addSubMenu(1, 100, 100, "hello");
subMenu.add(2, 3, 3, "hello");
subMenu.add(2, 4, 4, "hello world");
return true;
}
Context Menu :按键必须长按
在main.xml中创建btn1,btn2
public Button btn1;
public Button btn2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button)findViewById(R.id.btn1);
btn2 = (Button)findViewById(R.id.btn2);
registerForContextMenu(btn1);
registerForContextMenu(btn2);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
if(v==btn1){
menu.setHeaderTitle("这是1");
menu.add(200, 200, 200, "Context Menu 1");
menu.add(200, 201, 201, "Context Menu 2");
}
else if(v==btn2){
menu.setHeaderTitle("这是2");
menu.add(300, 300, 300, "C 1");
menu.add(300, 301, 301, "C 2");
}
super.onCreateContextMenu(menu, v, menuInfo);
}
参考:http://www.cnblogs.com/giuz/archive/2010/10/19/1855235.html