Option Menu 菜单 分为两种
1,图标菜单,这个菜单显示在屏幕的最鲜明,最多可以放6个菜单项,不支持复选框,单选按钮
2,扩展菜单,当图标菜单超过6个是,菜单只会显示5个菜单,第6个菜单会跳到扩展菜单,以后的菜单都会放到扩展菜单,并且垂直排列
3,有图有真相
所以图片都是系统自带的
多余6个图标菜单,会自动出现一个 更多
点击就可以看到扩展菜单
不多说了,直接上代码
package Rw.OptionsMenu;
import Android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class OptionsMenu extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public boolean onCreateOptionsMenu(Menu menu) {
//解释一下add这个方法
// 菜单群,群中Item的ID,排序的序号,文字显示;;;;后面是图标,我这里就一个群,可以自己添加啊
//还有一种菜单是在XML中定义,大家可以自己查看文档
menu.add(Menu.NONE, 1,8, "删除").setIcon(android.R.drawable.ic_menu_delete);
menu.add(Menu.NONE, 2, 2, "保存").setIcon(android.R.drawable.ic_menu_edit);
menu.add(Menu.NONE, 3, 6, "帮助").setIcon(android.R.drawable.ic_menu_help);
menu.add(Menu.NONE, 4, 1, "添加").setIcon( android.R.drawable.ic_menu_add);
menu.add(Menu.NONE, 5, 4, "详细").setIcon( android.R.drawable.ic_menu_info_details);
menu.add(Menu.NONE, 3, 3, "发送").setIcon(android.R.drawable.ic_menu_send);
menu.add(Menu.NONE, 7, 7, "分享").setIcon(android.R.drawable.ic_menu_share);
menu.add(Menu.NONE, 8, 5, "查找").setIcon(android.R.drawable.ic_menu_search);
menu.add(Menu.NONE, 9, 9, "拨号").setIcon(android.R.drawable.ic_menu_call);
// return true才会起作用
return true;
}
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()) {
case 1:
Toast.makeText(this, "删除", Toast.LENGTH_LONG).show();
break;
case 2:
Toast.makeText(this, "保存", Toast.LENGTH_LONG).show();
break;
case 3:
Toast.makeText(this, "帮助", Toast.LENGTH_LONG).show();
break;
case 4:
Toast.makeText(this, "添加", Toast.LENGTH_LONG).show();
break;
case 5:
Toast.makeText(this, "详细", Toast.LENGTH_LONG).show();
break;
case 6:
Toast.makeText(this, "发送", Toast.LENGTH_LONG).show();
break;
case 7:
Toast.makeText(this, "分享", Toast.LENGTH_LONG).show();
break;
case 8:
Toast.makeText(this, "查找", Toast.LENGTH_LONG).show();
break;
case 9:
Toast.makeText(this, "拨号", Toast.LENGTH_LONG).show();
break;
default:
break;
}
return false;
}
public void onOptionMenuClosed(Menu menu){
Toast.makeText(this, "选项菜单关闭了", Toast.LENGTH_LONG).show();
}
public boolean onPrepareOptionsMenu(Menu menu){
Toast.makeText(this, "选项菜单显示之前onPrepareOptionsMenu方法会被调用", Toast.LENGTH_LONG).show();
return true;
}
}