亲宝软件园·资讯

展开

Android中ContextMenu用法 Android中ContextMenu用法实例

鉴客 人气:0
想了解Android中ContextMenu用法实例的相关内容吗,鉴客在本文为您仔细讲解Android中ContextMenu用法的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Android,ContextMenu,下面大家一起来学习吧。

本文实例讲述了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程序设计有所帮助。

加载全部内容

相关教程
猜你喜欢
用户评论