Android中ContextMenu用法实例
作者:鉴客 时间:2023-02-12 20:09:49
本文实例讲述了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程序设计有所帮助。
标签:Android,ContextMenu
0
投稿
猜你喜欢
c# BackgroundWorker使用方法
2021-05-27 00:49:12
浅谈Java堆外内存之突破JVM枷锁
2022-10-19 19:46:50
WPF+Canvas实现平滑笔迹的示例代码
2021-10-06 06:35:13
JavaEE组件commons-fileupload实现文件上传、下载
2022-11-13 13:54:20
Mybatis-Plus支持GBase8s分页查询的实现示例
2021-11-21 14:33:30
C# 创建单例的多种方式
2023-03-17 05:23:48
C#实现基于IE内核的简单浏览器完整实例
2021-12-04 02:08:02
详细分析Java内存模型
2022-01-31 09:37:05
Android开发之菜单(menu)用法实例分析
2022-06-22 23:13:28
springboot aop配合反射统一签名验证实践
2023-08-04 08:14:07
Java 判断实体对象及所有属性是否为空的操作
2022-12-06 14:32:07
Spring Data JPA框架快速入门之自定义Repository接口
2023-05-23 03:49:48
c#中单例类与静态类的区别以及使用场景
2021-07-21 17:06:30
Spark调优多线程并行处理任务实现方式
2023-08-21 15:43:53
C#数据表格(DataGridView)控件的应用案例
2023-04-11 11:10:24
Java多线程实现复制文件
2023-03-21 07:54:17
C#中静态的深入理解
2023-10-18 01:28:27
Idea servlet映射方法优缺点对比
2023-06-12 23:59:34
javaweb页面附件、图片下载及打开(实现方法)
2023-11-25 05:33:44
c# Linq查询详解
2023-05-23 20:43:50