一个菜单中包括多个菜单项(MenuItem),在一个菜单中最多只会显示两行散列的菜单项,如果菜单项超过了六个,超过的会自动隐藏,会出现更多提示用户
在android中菜单一共分为三种:选项菜单(OptionsMenu)、上下文菜单(ContextMenu)、和子菜单(SubMenu).
一:选项菜单
这个暂时不需要配置文件
只需在Activity中进行完成
package com.qn.menu;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {//设置多个菜单项
menu.add(Menu.NONE, Menu.FIRST+1, 5, "删除")
.setIcon(android.R.drawable.ic_menu_delete);
menu.add(Menu.NONE,Menu.FIRST+2,6,"保存")
.setIcon(android.R.drawable.ic_menu_save);
menu.add(Menu.NONE,Menu.FIRST+3,7,"帮助")
.setIcon(android.R.drawable.ic_menu_help);
menu.add(Menu.NONE,Menu.FIRST+4,8,"添加")
.setIcon(android.R.drawable.ic_menu_add);
menu.add(Menu.NONE,Menu.FIRST+5,9,"详情")
.setIcon(android.R.drawable.ic_menu_info_details);
menu.add(Menu.NONE,Menu.FIRST+6,10,"发送")
.setIcon(android.R.drawable.ic_menu_send);
menu.add(Menu.NONE,Menu.FIRST+7,11,"编辑")
.setIcon(android.R.drawable.ic_menu_edit);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {//判断菜单项的操作
switch (item.getItemId()) {
case Menu.FIRST+1:
Toast.makeText(MainActivity.this, "您选择了删除",
Toast.LENGTH_SHORT).show();
break;
case Menu.FIRST+2:
Toast.makeText(MainActivity.this, "您选择了保存",
Toast.LENGTH_SHORT).show();
break;
case Menu.FIRST+3:
Toast.makeText(MainActivity.this, "您选择了帮助",
Toast.LENGTH_SHORT).show();
break;
case Menu.FIRST+4:
Toast.makeText(MainActivity.this, "您选择了添加",
Toast.LENGTH_SHORT).show();
break;
case Menu.FIRST+5:
Toast.makeText(MainActivity.this, "您选择了详情",
Toast.LENGTH_SHORT).show();
break;
case Menu.FIRST+6:
Toast.makeText(MainActivity.this, "您选择了发送",
Toast.LENGTH_SHORT).show();
break;
case Menu.FIRST+7:
Toast.makeText(MainActivity.this, "您选择了编辑",
Toast.LENGTH_SHORT).show();
break;
}
return false;
}
@Override
public void onOptionsMenuClosed(Menu menu) {//菜单项关闭触发的操作
Toast.makeText(MainActivity.this, "选项菜单关闭了",
Toast.LENGTH_SHORT).show();
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {//菜单项显示前触发的操作
Toast.makeText(MainActivity.this, "在菜单显示",
Toast.LENGTH_SHORT).show();
return true;
}
}
结果
2.上面的菜单项的内容都是固定出现的,根据MVC模式肯定要通过配置文件完成,下面是通过配置文件完成。直接在res/menu/mymenu.xml文件
1.准备好资源配置文件
?xml version="1.0" encoding="utf-8"?>
android:id="@+id/item01"
android:title="添加"
android:icon="@android:drawable/ic_menu_add"
/>
android:id="@+id/item02"
android:title="保存"
android:icon="@android:drawable/ic_menu_save"
/>
android:id="@+id/item03"
android:title="编辑"
android:icon="@android:drawable/ic_menu_edit"
/>
android:id="@+id/item04"
android:title="详情"
android:icon="@android:drawable/ic_menu_info_details"
/>
android:id="@+id/item05"
android:title="删除"
android:icon="@android:drawable/ic_menu_delete"
/>
android:id="@+id/item06"
android:title="发送"
android:icon="@android:drawable/ic_menu_send"
/>
android:id="@+id/item07"
android:title="帮助"
android:icon="@android:drawable/ic_menu_help"
/>
2.当准备好资源配置文件后
必须依靠MenuInflater类完成转换,此时只需要修改菜单的创建操作
package com.qn.m;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {//设置多个菜单项
super.getMenuInflater().inflate(R.menu.mymenu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {//判断菜单项的操作
switch (item.getItemId()) {
case Menu.FIRST+1:
Toast.makeText(MainActivity.this, "您选择了删除",
Toast.LENGTH_SHORT).show();
break;
case Menu.FIRST+2:
Toast.makeText(MainActivity.this, "您选择了保存",
Toast.LENGTH_SHORT).show();
break;
case Menu.FIRST+3:
Toast.makeText(MainActivity.this, "您选择了帮助",
Toast.LENGTH_SHORT).show();
break;
case Menu.FIRST+4:
Toast.makeText(MainActivity.this, "您选择了添加",
Toast.LENGTH_SHORT).show();
break;
case Menu.FIRST+5:
Toast.makeText(MainActivity.this, "您选择了详情",
Toast.LENGTH_SHORT).show();
break;
case Menu.FIRST+6:
Toast.makeText(MainActivity.this, "您选择了发送",
Toast.LENGTH_SHORT).show();
break;
case Menu.FIRST+7:
Toast.makeText(MainActivity.this, "您选择了编辑",
Toast.LENGTH_SHORT).show();
break;
}
return false;
}
@Override
public void onOptionsMenuClosed(Menu menu) {//菜单项关闭触发的操作
Toast.makeText(MainActivity.this, "选项菜单关闭了",
Toast.LENGTH_SHORT).show();
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {//菜单项显示前触发的操作
Toast.makeText(MainActivity.this, "在菜单显示",
Toast.LENGTH_SHORT).show();
return true;
}
}
结果
事例三:上下文菜单
上下文菜单非常类似于windows中右键菜单的操作形式,下面这个程序中我们要使用ListView与上下文菜单联系起来,但是要想使他们联系起来,则必须我们注册
registerForContextMenu(listView);//上下文菜单可以和listview融合在一起了
如下
只需要在Activity中完成
package com.qn.m;
import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class MainActivity extends Activity {
private String data[]=new String[]{"IJRQN","未来的名字","迟早会实现的","这次上天不会了",
"我喜欢你很久了"};
private ListView listView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
listView=new ListView(MainActivity.this);
listView.setAdapter(new ArrayAdapter(MainActivity.this,
android.R.layout.simple_expandable_list_item_1,data));
super.setContentView(listView);
registerForContextMenu(listView);//上下文菜单可以和listview融合在一起了
}
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case Menu.FIRST+1:
Toast.makeText(MainActivity.this, "您选择的是-添加联系人",
Toast.LENGTH_SHORT).show();
break;
case Menu.FIRST+2:
Toast.makeText(MainActivity.this, "您选择的是-查看详情",
Toast.LENGTH_SHORT).show();
break;
case Menu.FIRST+3:
Toast.makeText(MainActivity.this, "您选择的是-删除信息",
Toast.LENGTH_SHORT).show();
break;
case Menu.FIRST+4:
Toast.makeText(MainActivity.this, "您选择的是-另存为",
Toast.LENGTH_SHORT).show();
break;
case Menu.FIRST+5:
Toast.makeText(MainActivity.this, "您选择的是-编辑",
Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return super.onContextItemSelected(item);
}
@Override
public void onContextMenuClosed(Menu menu) {
Toast.makeText(MainActivity.this, "上下文菜单关闭了",
Toast.LENGTH_SHORT).show();
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("信息提示");
menu.add(Menu.NONE, Menu.FIRST+1, 1, "添加联系人");
menu.add(Menu.NONE, Menu.FIRST+2, 2, "查看详情");
menu.add(Menu.NONE, Menu.FIRST+3, 3, "删除信息");
menu.add(Menu.NONE, Menu.FIRST+4, 4, "另存为");
menu.add(Menu.NONE, Menu.FIRST+5, 5, "编辑");
}
}
结果
事例四同样上下文菜单也可以通过配置文件完成
1.和menu菜单相同直接在res/menu/mymenu.xml文件
?xml version="1.0" encoding="utf-8"?>
android:id="@+id/item02"
android:title="添加联系人"
/>
android:id="@+id/item03"
android:title="查看详情"
/>
android:id="@+id/item04"
android:title="删除信息"
/>
android:id="@+id/item05"
android:title="另存为"
/>
android:id="@+id/item06"
android:title="编辑"
/>
2.在Activity中完成代码
package com.qn.m;
import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class MainActivity extends Activity {
private String data[]=new String[]{"IJRQN","未来的名字","迟早会实现的","这次上天不会了",
"我喜欢你很久了"};
private ListView listView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
listView=new ListView(MainActivity.this);
listView.setAdapter(new ArrayAdapter(MainActivity.this,
android.R.layout.simple_expandable_list_item_1,data));
super.setContentView(listView);
registerForContextMenu(listView);//上下文菜单可以和listview融合在一起了
}
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case Menu.FIRST+1:
Toast.makeText(MainActivity.this, "您选择的是-添加联系人",
Toast.LENGTH_SHORT).show();
break;
case Menu.FIRST+2:
Toast.makeText(MainActivity.this, "您选择的是-查看详情",
Toast.LENGTH_SHORT).show();
break;
case Menu.FIRST+3:
Toast.makeText(MainActivity.this, "您选择的是-删除信息",
Toast.LENGTH_SHORT).show();
break;
case Menu.FIRST+4:
Toast.makeText(MainActivity.this, "您选择的是-另存为",
Toast.LENGTH_SHORT).show();
break;
case Menu.FIRST+5:
Toast.makeText(MainActivity.this, "您选择的是-编辑",
Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return super.onContextItemSelected(item);
}
@Override
public void onContextMenuClosed(Menu menu) {
Toast.makeText(MainActivity.this, "上下文菜单关闭了",
Toast.LENGTH_SHORT).show();
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("信息提示");
getMenuInflater().inflate(R.menu.mymenu, menu);
}
}
结果
三子菜单,当系统定义好菜单后也可以为每一个菜单里面定义多个子菜单
如下
完全是在Activity中完成
package com.qn.m;
import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class MainActivity extends Activity {
private String data[]=new String[]{"IJRQN","未来的名字","迟早会实现的","这次上天不会了",
"我喜欢你很久了"};
private ListView listView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
listView=new ListView(MainActivity.this);
listView.setAdapter(new ArrayAdapter(MainActivity.this,
android.R.layout.simple_expandable_list_item_1,data));
super.setContentView(listView);
registerForContextMenu(listView);//上下文菜单可以和listview融合在一起了
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
SubMenu fileMenu=menu.addSubMenu("文件");
SubMenu editMenu=menu.addSubMenu("编辑");
fileMenu.add(Menu.NONE,Menu.FIRST+1,1,"新建");
fileMenu.add(Menu.NONE,Menu.FIRST+2,2,"打开");
fileMenu.add(Menu.NONE,Menu.FIRST+3,3,"保存");
fileMenu.add(Menu.NONE,Menu.FIRST+4,4,"撤销");
fileMenu.add(Menu.NONE,Menu.FIRST+5,5,"恢复");
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case Menu.FIRST+1:
Toast.makeText(MainActivity.this, "添加联系人",
Toast.LENGTH_SHORT).show();
break;
case Menu.FIRST+2:
Toast.makeText(MainActivity.this, "查看详情",
Toast.LENGTH_SHORT).show();
break;
case Menu.FIRST+3:
Toast.makeText(MainActivity.this, "删除信息",
Toast.LENGTH_SHORT).show();
break;
case Menu.FIRST+4:
Toast.makeText(MainActivity.this, "另存为",
Toast.LENGTH_SHORT).show();
break;
case Menu.FIRST+5:
Toast.makeText(MainActivity.this, "编辑",
Toast.LENGTH_SHORT).show();
break;
}
return super.onContextItemSelected(item);
}
}
结果
四:子菜单也可以通过配置文件完成
在res/menu/filemenu.xml和res/menu/editmenu.xml文件
?xml version="1.0" encoding="utf-8"?>
">
android:id="@+id/item01"
android:title="新建"
/>
android:id="@+id/item02"
android:title="打开"
/>
android:id="@+id/item03"
android:title="保存"
/>
?xml version="1.0" encoding="utf-8"?>
">
android:id="@+id/item04"
android:title="撤销"
/>
android:id="@+id/item05"
android:title="恢复"
/>
2.在Activity完成代码
package com.qn.m;
import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class MainActivity extends Activity {
private String data[]=new String[]{"IJRQN","未来的名字","迟早会实现的","这次上天不会了",
"我喜欢你很久了"};
private ListView listView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
listView=new ListView(MainActivity.this);
listView.setAdapter(new ArrayAdapter(MainActivity.this,
android.R.layout.simple_expandable_list_item_1,data));
super.setContentView(listView);
registerForContextMenu(listView);//上下文菜单可以和listview融合在一起了
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
SubMenu fileMenu=menu.addSubMenu("文件");
SubMenu editMenu=menu.addSubMenu("编辑");
getMenuInflater().inflate(R.menu.filemenu, fileMenu);
getMenuInflater().inflate(R.menu.editmenu, editMenu);
return super.onCreateOptionsMenu(menu);
}
}
结果