Android中文API(97)—— ContextMenu
正文 一、结构 public interfaceContextMenu implementsMenu android.view.ContextMenu 二、概述 扩展自Menu的上下文菜单提供了修改上下文菜单头(header)的功能。(译者注:当一个视图注册了上下文菜单时,执行一个在该对象上长按(2秒)的动作,将出现一个具有相关功能的浮动菜单。) 上下文菜单不支持菜单项的快捷方式和图标。 当执行长按上下文菜单时,大多数情况会调用registerForContextMenu(View)函数和重写执行onCreateContextMenu(ContextMenu, View, ContextMenu.ContextMenuInfo)函数。(译者注:因为要创建一个上下文菜单,你必须重写这个活动的上下文回调函数onCreateContextMenu()并且通过registerForContextMenu(View)为其注册上下文菜单。) 三、内部类 interface ContextMenu.ContextMenuInfo 获得更多关于创建上下文菜单的信息。(译者注:例如:AdapterViews使用这个类可以精确选择adapter的位置来启动上下文菜单。) 四、公共方法 public abstract voidclearHeader() 清除上下文菜单头的信息。(译者注:包括图片和文字信息 Menu.clearHeader();后 ) public abstract ContextMenusetHeaderIcon(Drawable icon) 为上下文菜单头设置图标 参数 icon你要使用的Drawable 返回值 调用你设置修改的上下文菜单 public abstract ContextMenusetHeaderIcon(int iconRes) 设置上下文菜单头图标为指定的资源id 参数 iconRes 你要使用的图标资源的目录 (译者注:把图标放入res/drawable/目录下,R文件会自动生成对应项。设置方法如menu.setHeaderIcon(R.drawable.webtext) 这个上下文菜单头是没有设置图标的 这个上下文菜单头是设置了图标的 ) 返回值 调用你设置修改过的上下文菜单 public abstract ContextMenusetHeaderTitle(int titleRes) 通过资源标识符为上下文菜单头的标题栏设置文字。(译者注:需要在res/string中先设置一段你需要的文字,如:<stringname="titletest">这是一段测试文字</string> 然后通过R文件索引到这段文字,menu.setHeaderTitle(R.string.titletest)) 参数 titleRes 所需文字资源的索引 返回值 调用你设置修改过的上下文菜单 public abstract ContextMenusetHeaderTitle(CharSequence title) 设置上下文菜单的标题,显示在标题栏 参数 title标题要显示的文字 返回值 调用你设置修改过的上下文菜单 public abstract ContextMenusetHeaderView(View view) 设置View到上下文菜单头上。将替代上下文菜单头的图标和标题(或者替代你之前设置的headerView) 参数 view上下文菜单头要使用的View 返回值 调用你设置修改过的上下文菜单内容 五、补充 文章精选: Android的上下文菜单:Context Menu android的ContextMenu android上下文菜单Context Menu 代码示例: Test_Contextmenu.java public class Test_Contextmenu extends Activity{ /** Calledwhentheactivityisfirstcreated. */ @Override public void onCreate(BundlesavedInstanceState){ super .onCreate(savedInstanceState); setContentView(R.layout.main); TextViewtxt1 = (TextView) this .findViewById(R.id.txt1); this .registerForContextMenu(txt1); TextViewtxt2 = (TextView) this .findViewById(R.id.txt2); this .registerForContextMenu(txt2); } // 重写 onCreateContextMenu 用以创建上下文菜单 @Override public void onCreateContextMenu(ContextMenumenu,Viewv, ContextMenuInfomenuInfo){ super .onCreateContextMenu(menu,v,menuInfo); // 创建R.id.txt1的上下文菜单 if (v == (TextView) this .findViewById(R.id.txt1)){ menu.setHeaderIcon(R.drawable.icon); menu.setHeaderTitle(R.string.titletest); // menu.clearHeader(); // 第一个参数:组ID // 第二个参数:菜单项ID // 第三个参数:顺序号 // 第四个参数:菜单项上显示的内容 menu.add( 1 , 0 , 0 , " 菜单1 " ); menu.add( 1 , 1 , 1 , " 菜单2 " ).setCheckable( true ); // 增加一个√选项 } // 创建R.id.txt2的上下文菜单(多级) else if (v == (TextView) this .findViewById(R.id.txt2)){ // ContextMenu.addSubMenu("菜单名称") - 用来添加子菜单。子菜单其实就是一个特殊的菜单 SubMenusub1 = menu.addSubMenu( " 父菜单1 " ); sub1.setHeaderIcon(R.drawable.folder); sub1.add( 0 , 0 , 0 , " 菜单1 " ); sub1.add( 0 , 1 , 1 , " 菜单2 " ); sub1.setGroupCheckable( 1 , true , true ); SubMenusub2 = menu.addSubMenu( " 父菜单2 " ); sub2.setIcon(R.drawable.text); sub2.add( 1 , 0 , 0 , " 菜单3 " ); sub2.add( 1 , 1 , 1 , " 菜单4 " ); sub2.setGroupCheckable( 1 , true , true ); } } } main.xml <? xmlversion="1.0"encoding="utf-8" ?> < LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android" android:orientation ="vertical" android:layout_width ="fill_parent" android:layout_height ="fill_parent" > < TextView android:id ="@+id/txt1" android:layout_width ="fill_parent" android:layout_height ="wrap_content" android:text ="请长按触发(txt1)" /> < TextView android:id ="@+id/txt2" android:layout_width ="fill_parent" android:layout_height ="wrap_content" android:text ="请长按触发(txt2)" /> </ LinearLayout > strings.xml <? xmlversion="1.0"encoding="utf-8" ?> < resources > < string name ="hello" > HelloWorld,Test_Contextmenu! </ string > < string name ="app_name" > Test_Contextmenu </ string > < string name ="titletest" > 这是一段测试文字 </ string > </resources> 本文转自over140 51CTO博客,原文链接:http://blog.51cto.com/over140/582398,如需转载请自行联系原作者