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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.menudemo.ContextMenuDemo" > <ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
></ListView> </RelativeLayout>
源代码:
package com.example.menudemo; import java.util.ArrayList;
import java.util.List; import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
/**
* ContextMenu的组成
* 标题以及标题图标
* 菜单内容
* 菜单内容的点击响应事件
*
* ContextMenu与OptionMenu的区别:
* OptionMenu对应的是activity,一个activity只能拥有一个选项菜单
* ContextMenu对应的是View,每个View都可以设置上下文菜单
* 一般情况下ContextMenu常用于ListView或者GridView
* ContextMenu创建方法
* 首先给View注册上下文菜单registerForContextMenu()
* 添加上下文菜单内容onCreateContextMenu()
* -通过代码动态添加
* -加载xml文件中的菜单项
* 设置菜单点击后响应事件onContextItemSelected()
* @author Administrator
*
*/
public class ContextMenuDemo extends Activity { private ListView listview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.context_menu);
showListView();
this.registerForContextMenu(listview);//给ListView注册上下文菜单
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
super.onCreateContextMenu(menu, v, menuInfo);
//设置Menu显示的内容
menu.setHeaderIcon(R.drawable.ic_launcher);
menu.setHeaderTitle("file oprate");
menu.add(1, 1, 1, "copy");
menu.add(1, 2, 1, "cut");
menu.add(1, 3, 1, "delete");
menu.add(1, 4, 1, "rename");
/**加载xml文件中的菜单项
* getMenuInflater().inflate(R.menu.main,menu);
*/ }
@Override
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch(item.getItemId()){
case 1:
Toast.makeText(this, "copy", Toast.LENGTH_SHORT).show();
break;
case 2:
Toast.makeText(this, "cut", Toast.LENGTH_SHORT).show();
break;
case 3:
Toast.makeText(this, "delete", Toast.LENGTH_SHORT).show();
break;
case 4:
Toast.makeText(this, "rename", Toast.LENGTH_SHORT).show();
break;
} return super.onContextItemSelected(item); }
/**
* 设置ListView显示的内容
*/
private void showListView(){
listview = (ListView) findViewById(R.id.listView);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,getData());
listview.setAdapter(adapter);
}
/**
* 构造ListView显示的数据
* @return
*/
private List<String> getData() {
// TODO Auto-generated method stub
List<String> list = new ArrayList<String>();
for(int i = 0;i<5;i++){
list.add("file"+(i+1));
}
return list;
} }