Android中ContextMenu用法实例
程序员文章站
2022-06-19 19:49:54
本文实例讲述了android中contextmenu用法。分享给大家供大家参考。具体如下:
main.xml文件如下:
本文实例讲述了android中contextmenu用法。分享给大家供大家参考。具体如下:
main.xml文件如下:
<?xml version="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" > <listview android:id="@android:id/list" android:layout_width="fill_parent" android:layout_height="fill_parent"></listview> </linearlayout>
simple_list_item_1.xml文件如下:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <textview android:id="@+id/label" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textsize="30sp"></textview> </linearlayout>
menudemoactivity.java如下:
package com.yyl; import android.app.listactivity; 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; public class menudemoactivity extends listactivity { public static final int eight_id = menu.first+1; public static final int sixteen_id = menu.first+2; public static final int twenty_four_id = menu.first+3; public static final int two_id = menu.first+4; public static final int thirty_two_id = menu.first+5; public static final int forty_id = menu.first+6; public static final int one_id = menu.first+7; string[] items={"lorem", "ipsum", "dolor", "sit", "amet", "consectetuer", "adipiscing", "elit", "morbi", "vel", "ligula", "vitae", "arcu", "aliquet", "mollis", "etiam", "vel", "erat", "placerat", "ante", "porttitor", "sodales", "pellentesque", "augue", "purus"}; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); setlistadapter(new arrayadapter(this,r.layout.simple_list_item_1,r.id.label,items)); //注册contextview到view中 registerforcontextmenu(getlistview()); } //重写该方法,生成contextmenu菜单 @override public void oncreatecontextmenu(contextmenu menu, view v, contextmenuinfo menuinfo) { this.populatemenu(menu); super.oncreatecontextmenu(menu, v, menuinfo); } private void populatemenu(menu menu) { menu.add(menu.none, one_id, menu.none, "1 pixel"); menu.add(menu.none, two_id, menu.none, "2 pixels"); menu.add(menu.none, eight_id, menu.none, "8 pixels"); menu.add(menu.none, sixteen_id, menu.none, "16 pixels"); menu.add(menu.none, twenty_four_id, menu.none, "24 pixels"); menu.add(menu.none, thirty_two_id, menu.none, "32 pixels"); menu.add(menu.none, forty_id, menu.none, "40 pixels"); } }
希望本文所述对大家的android程序设计有所帮助。
上一篇: js 浮动层菜单收藏
下一篇: 饭后一颗口香糖,你不知道的口香糖的好处