绳锯木断,水滴石穿。—— 罗大经《鹤林玉露》
本讲内容:菜单 Android Menu
在Android中,菜单被分为如下三种:
选项菜单(OptionsMenu):
上下文菜单(ContextMenu):长按跳出菜单
子菜单(SubMenu):点击选项菜单将弹出悬浮窗口显示子菜单项。
一、ContextMenu与OptionMenu的区别
1、OptionMenu对应的是activity,一个activity只能拥有一个选项菜单。
2、ContextMenu对应的是View,每个View都可以设置上下文菜单。
3、一般情况下ContextMenu常用于ListView或者GridView.
ContextMenu的组成:1、标题以及标题图标 2、菜单内容 3、菜单内容的点击事件
ContextMenu创建方法:首先给View注册上下文菜单registerForContextMenu(),然后添加菜单项。
示例一:选项菜单(通过xml加载)注意:api>=11 菜单不支持显示图标
下面是res/menu/main.xml 文件:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/menu_set"
android:title="@string/action_set"/>
<item
android:id="@+id/menu_help"
android:title="@string/action_help"/>
</menu>
注意:如果不引用@string可能会导致程序总无法自动在R.java中生成相应的ID。
下面是MainActivity.java主界面文件:
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
// 创建选项菜单
public boolean onCreateOptionsMenu(Menu menu) {
// 通过getMenuInflater()方法得到MenuInflater对象,再调用它的inflate()方法就可以给当前活动创建菜单了
getMenuInflater().inflate(R.menu.main, menu);
// 返回true表示允许创建的菜单显示出来
return true;
}
// 选项菜单响应事件
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_set:
Toast.makeText(this, "设置", Toast.LENGTH_LONG).show();
break;
case R.id.menu_help:
Toast.makeText(this, "帮助", Toast.LENGTH_LONG).show();
break;
}
return true;
}
}
示例二:选项菜单(通过java加载)
下面是MainActivity.java主界面文件:
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(1, 100, 1, "设置");
menu.add(1, 101, 1, "帮助");
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 100:
//跳转activity
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
item.setIntent(intent);//设置点击意图
break;
case 101:
Toast.makeText(MainActivity.this, "点击了帮助", Toast.LENGTH_SHORT).show();
break;
}
return super.onOptionsItemSelected(item);
}
}
示例三:上下文菜单
下面是res/layout/activity_main.xml 布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@+id/id_listView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
下面是res/menu/main.xml 文件:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/context_menu_item1"
android:orderInCategory="100"
android:showAsAction="never"
android:title="复制"/>
<item
android:id="@+id/context_menu_item2"
android:orderInCategory="100"
android:showAsAction="never"
android:title="粘贴"/>
<item
android:id="@+id/context_menu_item3"
android:orderInCategory="100"
android:showAsAction="never"
android:title="剪切"/>
<item
android:id="@+id/context_menu_item4"
android:orderInCategory="100"
android:showAsAction="never"
android:title="重命名"/>
</menu>
下面是MainActivity.java主界面文件:
public class MainActivity extends Activity {
private ListView listView;
private List<String> listData;
ArrayAdapter<String> adapter;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView=(ListView) findViewById(R.id.id_listView);
adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,getData());
listView.setAdapter(adapter);
//注册上下文菜单
this.registerForContextMenu(listView);
}
/**
* 创建上下文菜单
*/
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
//设置Menu显示内容
menu.setHeaderTitle("文件操作");
menu.setHeaderIcon(R.drawable.ic_launcher);
// menu.add(1, 1, 1, "复制");
// menu.add(1, 2, 1, "粘贴");
// menu.add(1, 3, 1, "剪切");
// menu.add(1, 4, 1, "重命名");
getMenuInflater().inflate(R.menu.main, menu);
}
/**
* 上下文菜单响应事件
*/
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.context_menu_item1:
Toast.makeText(MainActivity.this, "点击复制",Toast.LENGTH_SHORT).show();
break;
case R.id.context_menu_item2:
Toast.makeText(MainActivity.this, "点击粘贴",Toast.LENGTH_SHORT).show();
break;
case R.id.context_menu_item3:
Toast.makeText(MainActivity.this, "点击剪切",Toast.LENGTH_SHORT).show();
break;
case R.id.context_menu_item4:
Toast.makeText(MainActivity.this, "点击重命名",Toast.LENGTH_SHORT).show();
break;
}
return super.onContextItemSelected(item);
}
/**
* listView数据源
*/
private List<String> getData(){
listData=new ArrayList<String>();
for(int i=0;i<5;i++){
listData.add("文件"+(i+1));
}
return listData;
}
}
示例四:SubMenu
下面是MainActivity.java主界面文件:
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public boolean onCreateOptionsMenu(Menu menu) {
SubMenu file = menu.addSubMenu("文件");
SubMenu edit = menu.addSubMenu("编辑");
file.setHeaderTitle("文件操作");//子菜单标题
file.setHeaderIcon(R.drawable.ic_launcher);//子菜单图标
file.add(1, 1, 1, "新建");
file.add(1, 2, 1, "打开");
file.add(1, 3, 1, "保存");
edit.setHeaderTitle("编辑操作");
edit.setHeaderIcon(R.drawable.ic_launcher);
edit.add(2, 1, 1, "复制");
edit.add(2, 2, 1, "粘贴");
edit.add(2, 3, 1, "剪切");
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getGroupId()==1){
switch (item.getItemId()) {
case 1:
Toast.makeText(this, "点击了新建",Toast.LENGTH_SHORT).show();
break;
case 2:
Toast.makeText(this, "点击了粘贴",Toast.LENGTH_SHORT).show();
break;
case 3:
Toast.makeText(this, "点击了剪切",Toast.LENGTH_SHORT).show();
break;
}
}else if(item.getGroupId()==2){
switch (item.getItemId()) {
case 1:
Toast.makeText(this, "点击了新建",Toast.LENGTH_SHORT).show();
break;
case 2:
Toast.makeText(this, "点击了粘贴",Toast.LENGTH_SHORT).show();
break;
case 3:
Toast.makeText(this, "点击了剪切",Toast.LENGTH_SHORT).show();
break;
}
}
return super.onOptionsItemSelected(item);
}
}
示例五:SubMenu(xml)
下面是res/menu/main.xml 文件:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:showAsAction="never"
android:title="文件">
<menu>
<item
android:id="@+id/new_file"
android:showAsAction="never"
android:title="新建"/>
<item
android:id="@+id/open_file"
android:showAsAction="never"
android:title="打开"/>
<item
android:id="@+id/save_file"
android:showAsAction="never"
android:title="保存"/>
</menu>
</item>
<item
android:showAsAction="never"
android:title="编辑">
<menu>
<item
android:id="@+id/c_edit"
android:showAsAction="never"
android:title="复制"/>
<item
android:id="@+id/v_edit"
android:showAsAction="never"
android:title="粘贴"/>
<item
android:id="@+id/x_edit"
android:showAsAction="never"
android:title="剪切"/>
</menu>
</item>
</menu>
下面是MainActivity.java主界面文件:
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.new_file:
Toast.makeText(this, "点击了新建",Toast.LENGTH_SHORT).show();
break;
case R.id.open_file:
Toast.makeText(this, "点击了打开",Toast.LENGTH_SHORT).show();
break;
case R.id.save_file:
Toast.makeText(this, "点击了保存",Toast.LENGTH_SHORT).show();
break;
case R.id.c_edit:
Toast.makeText(this, "点击了复制",Toast.LENGTH_SHORT).show();
break;
case R.id.v_edit:
Toast.makeText(this, "点击了粘贴",Toast.LENGTH_SHORT).show();
break;
case R.id.x_edit:
Toast.makeText(this, "点击了剪切",Toast.LENGTH_SHORT).show();
break;
}
return super.onOptionsItemSelected(item);
}
}
Take your time and enjoy it