3.菜单
选项菜单
public boolean onCreateOptionsMenu(Menu menu) {
//分组,菜单选项id,菜单编号,菜单标题
menu.add(Menu.NONE, 0, 0, "保存")
.setIcon(android.R.drawable.ic_menu_save);
menu.add(Menu.NONE, 1, 0, "删除")
.setIcon(android.R.drawable.ic_menu_delete);
//子菜单
SubMenu helpMenu=menu.addSubMenu("帮助")
.setIcon(android.R.drawable.ic_menu_help);
helpMenu.add("保存介绍");
helpMenu.add("出错方案");
return super.onCreateOptionsMenu(menu);
}
在资源文件中定义菜单
<item
android:id="@+id/save"
android:title="保存"
></item>
<item
android:id="@+id/delete"
android:title="删除"
></item>
<item
android:id="@+id/help"
android:title="帮助"
>
<!-- 添加子菜单 -->
<menu>
<item android:id="@+id/sava1" android:title="保存信息"></item>
<item android:id="@+id/delete1" android:title="删除信息"></item>
</menu>
</item>
public boolean onCreateOptionsMenu(Menu menu) {
//创建菜单资源解析对象
MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.options, menu);
return super.onCreateOptionsMenu(menu);
}
//判断菜单项的操作
public boolean onOptionsItemSelected(MenuItem item) {
int id=item.getItemId();
if(id==R.id.sava1){
Toast.makeText(this, "选择了保存", 5000).show();
}
if(id==R.id.delete1){
Toast.makeText(this, "选择了删除", 5000).show();
}