Android Menus
2010-07-15 20:44:00 来源:WEB开发网}
通过XML文件添加:
在res/menu/下新建options_menu.xml文件:
Xml代码
在onCreateOptionsMenu()方法中,我们这样写:
Java代码
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
return true;
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
return true;
}
2.Context Menu.
它需要注册到某个View对象上,当长按这个View大概2秒时间,会出现这个Context Menu。为某个View注册Context Menu是用registerForContextMenu(View view)方法来实现。Context Menu在显示前都会调用onCreateContextMenu来生成menu。onContextItemSelected用来处理选中的菜单项。注意:Context Menu不支持icons和shortcut keys.
Java代码
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.add(0, EDIT_ID, 0, "Edit");
menu.add(0, DELETE_ID, 0, "Delete");
}
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo)
更多精彩
赞助商链接