Android中ContextMenu的使用

1、配置布局文件


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>

 

2、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的使用

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭