Android里面也可用XML定义菜单资源,菜单分两种,一种是OptionMenu(点系统"Menu"键出来),另一种是ContextMenu必须长按才能出来;
optionMenu菜单的使用
1,创建选项菜单; onCreateOptionsMenu(Menu menu)
//创建选项菜单的方法 @Override public boolean onCreateOptionsMenu(Menu menu) { //menu.add(groupId, itemId, order, title) //goupid 菜单项所在组的标示符 //itemid 唯一表示的ID //order 菜单栏项的显示顺序 //title 菜单项相似的文本 MenuItem menu1 = menu.add(0, 0, 0, "item1"); menu1.setAlphabeticShortcut('a');//分配快捷键的方法 menu1.setIcon(R.drawable.ic_launcher); //设置图标 MenuItem menu2 = menu.add(0, 1, 1, "item2"); menu1.setAlphabeticShortcut('b');//分配快捷键的方法 menu1.setIcon(R.drawable.ic_launcher); //设置图标 MenuItem menu3 = menu.add(0, 2, 2, "item3"); menu1.setAlphabeticShortcut('c');//分配快捷键的方法 menu1.setIcon(R.drawable.ic_launcher); //设置图标 //使用简单方式创建三个菜单栏 menu.add(0,4,4,"item4"); menu.add(0,5,5,"item5"); menu.add(0,6,6,"item6"); return true; }
2,添加选项菜单的监听器onOptionsItemSelected(MenuItem item);
@Override//选项菜单的监听器 public boolean onOptionsItemSelected(MenuItem item) { //MenuChoice方法接收Menuite方法的一个参数,并检查其ID确定被选中的菜单项 int id = item.getItemId(); switch (id) { case 0: Toast.makeText(this, "选中item1", 1000).show(); break; case 1: Toast.makeText(this, "选中item2", 1000).show(); break; case 2: Toast.makeText(this, "选中item3", 1000).show(); break; case 4: Toast.makeText(this, "选中item4", 1000).show(); break; case 5: Toast.makeText(this, "选中item5", 1000).show(); break; case 6: Toast.makeText(this, "选中item6", 1000).show(); break; } return false; }
ContextMenu菜单的使用;
上下文菜单的使用必须是长按才会出来
1,定义布局上下文菜单的布局文件menu_contextemnu.xml
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/rename" android:title="修改名字" /> <item android:id="@+id/dele" android:title="删除" /> <item android:id="@+id/open" android:title="打开" /> <item android:id="@+id/detailed" android:title="详细" /> </menu>
2,定义主配置文件 acticity_main.xml
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onclick" android:text="上下文菜单" />
3,java类的使用
//上下文菜单的使用 public class MainActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); this.setContentView(R.layout.activity_main); //获得按钮 Button btn=(Button)this.findViewById(R.id.btn); btn.setOnCreateContextMenuListener(this); //上下文菜单的使用必须要先注册组件 this.registerForContextMenu(btn); } public void onclick(View v){ } @Override//创建上下文菜单 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); getMenuInflater().inflate(R.layout.menu_contextemnu, menu); } @Override//创建上下文监听器 public boolean onContextItemSelected(MenuItem item) { int id = item.getItemId(); switch (id) { case R.id.rename: Toast.makeText(this, "重命名", 1000).show(); break; case R.id.dele: Toast.makeText(this, "删除", 1000).show(); break; case R.id.detailed: Toast.makeText(this, "详细", 1000).show(); break; case R.id.open: Toast.makeText(this, "打开", 1000).show(); break; } return false; } }
3, 选项菜单和上下文菜单监听器;
onMenuItemSelected可以同时使用在有少量的选项菜单和上下文的情况,下面一个方法就搞定, 如果出现上下文监听器(onContextItemSelected),选项监听器(onOptionsItemSelected) ,onMenuItemSelected 则只会执行onMenuItemSelected 监听器
@Override // 菜单被选中的时候会执行 public boolean onMenuItemSelected(int featureId, MenuItem item) { // TODO Auto-generated method stub return true; }
相关推荐
源程序有关android平台上menu的使用示例,仅供参考
Android菜单、对话框使用实例源码,,基本使用
android fragment 使用以及嵌套使用 底部菜单和顶部菜单,类似微信和QQ的分页菜单
android自定义菜单使用技巧,其中还包含QuickContactBadge控件的使用
Android 菜单筛选器
android分类列表菜单
android ExpandableListView三级菜单的使用,多级菜单下拉与隐藏,ExpandableListView的简单使用
android底部菜单栏demo
Android Studio 底部自定义菜单,菜单上有消息数提醒,很实用
android 菜单 menu android 菜单 menu android 菜单 menu android 菜单 menu android 菜单 menu
最新最实用的android菜单界面布局,不下会后悔的
android 扇形菜单(属性动画实现)
android中实现点击,下拉菜单栏功能
android Menu菜单。android Menu菜单。android Menu菜单。android Menu菜单。
仿微信菜单导航及左右切换,仿支付宝菜单效果,代码简单实用
简单实用的Android系统菜单与自定义菜单的实现方法,并对菜单的调用机制进行了全面验证。 开发环境:XP3+Eclipse+Android2.2+JDK6.0 测试环境:Android2.2模拟器
文档中包括Android实现上下文菜单、下拉菜单、选项菜单代码以及截图,亲测可用!!!
Android 通用菜单栏实现、可以稍微变通,满足大部分软件开发需要。
android的菜单实现代码,实现菜单的多页显示