目錄 搜尋
android Manifest Manifest.permission Manifest.permission_group android.accessibilityservice AccessibilityService android.accounts Account android.app NotificationManager android.bluetooth BluetoothAdapter BluetoothClass BluetoothClass.Device BluetoothClass.Device.Major BluetoothClass.Service BluetoothDevice BluetoothServerSocket BluetoothSocket android.content SharedPreferences android.database.sqlite SQLiteCursorDriver SQLiteOpenHelper android.graphics Bitmap android.location LocationListener Geocoder GpsStatus GpsStatus.Listener GpsStatus.NmeaListener GpsSatellite android.media AudioFormat AsyncPlayer AudioRecord AudioRecord.OnRecordPositionUpdateListener ThumbnailUtils AudioManager android.net TrafficStats MailTo LocalSocket android.os AsyncTask AsyncTask.Status CountDownTimer Message MessageQueue HandlerThread android.text Html android.util JsonWriter android.view ContextMenu ContextMenu.ContextMenuInfo Display ViewManager View ViewStub ViewTreeObserver ViewParent WindowManager GestureDetector Gravity MenuInflater ScaleGestureDetector SoundEffectConstants android.view.inputmethod InputConnection InputMethod InputMethodSession BaseInputConnection InputMethodManager android.widget AbsListView AbsListView.LayoutParams AbsListView.OnScrollListener AbsListView.RecyclerListener AbsoluteLayout AbsoluteLayout.LayoutParams AbsSeekBar AbsSpinner AdapterView AdapterView.AdapterContextMenuInfo AdapterView.OnItemLongClickListener AdapterView.OnItemSelectedListener AdapterView.OnItemClickListener AnalogClock BaseAdapter BaseExpandableListAdapter Button CheckBox CheckedTextView Checkable Chronometer Chronometer.OnChronometerTickListener CompoundButton CompoundButton.OnCheckedChangeListener CursorAdapter CursorTreeAdapter DatePicker DatePicker.OnDateChangedListener DialerFilter DigitalClock EditText Filter Filter.FilterListener Filter.FilterResults ExpandableListAdapter ExpandableListView.OnChildClickListener ExpandableListView.OnGroupClickListener ExpandableListView.OnGroupCollapseListener ExpandableListView.OnGroupExpandListener Filterable Gallery Gallery.LayoutParams GridView GridLayout GridLayout.Alignment RadioGroup ImageView ImageView.ScaleType HorizontalScrollView ImageButton ImageSwitcher FilterQueryProvider ListAdapter ListView MediaController MultiAutoCompleteTextView MultiAutoCompleteTextView.CommaTokenizer MultiAutoCompleteTextView.Tokenizer QuickContactBadge RadioButton RatingBar RatingBar.OnRatingBarChangeListener RelativeLayout RemoteViews ResourceCursorAdapter ResourceCursorTreeAdapter Scroller ScrollView SearchView SearchView.OnCloseListener SearchView.OnQueryTextListener SearchView.OnSuggestionListener SeekBar SeekBar.OnSeekBarChangeListener SimpleAdapter SimpleAdapter.ViewBinder SimpleCursorAdapter SimpleCursorAdapter.CursorToStringConverter SimpleCursorAdapter.ViewBinder SimpleCursorTreeAdapter SimpleCursorTreeAdapter.ViewBinder SimpleExpandableListAdapter SlidingDrawer SlidingDrawer.OnDrawerCloseListener SlidingDrawer.OnDrawerOpenListener SlidingDrawer.OnDrawerScrollListener Spinner SpinnerAdapter WrapperListAdapter TabHost TabHost.TabSpec TextView TimePicker TimePicker.OnTimeChangedListener Toast TableLayout TableLayout.LayoutParams TableRow TableRow.LayoutParams TabWidget TextSwitcher ToggleButton TwoLineListItem VideoView ViewAnimator ViewFlipper ViewSwitcher ViewSwitcher.ViewFactory ZoomButtonsController ZoomButtonsController.OnZoomListener ZoomButton ZoomControls dalvik.system DexFile
文字


ContextMenu

版本:Android 2.3 r1

 

结构

继承关系

public interface ContextMenu implements Menu

 

android.view.ContextMenu

 

类概述

     扩展自Menu的上下文菜单提供了修改上下文菜单头(header)的功能。(译者注:当一个视图注册了上下文菜单时,执行一个在该对象上长按(2秒)的动作,将出现一个具有相关功能的浮动菜单。)

     上下文菜单不支持菜单项的快捷方式和图标。

     当执行长按上下文菜单时,大多数情况会调用registerForContextMenu(View) 函数和重写执行onCreateContextMenu(ContextMenu, View, ContextMenu.ContextMenuInfo)函数。(译者注:因为要创建一个上下文菜单,你必须重写这个活动的上下文回调函数onCreateContextMenu() 并且 通过registerForContextMenu(View) 为其注册上下文菜单。)

 

内部类

         interface  ContextMenu.ContextMenuInfo

获得更多关于创建上下文菜单的信息。( 译者注:例如:AdapterViews 使用这个类可以精确选择adapter的位置来启动上下文菜单。)

 

公共方法

         public abstract void clearHeader ()

清除上下文菜单头的信息。(译者注:包括图片和文字信息

Menu.clearHeader();

 

public abstract ContextMenu setHeaderIcon (Drawable icon)

为上下文菜单头设置图标

参数

icon          你要使用的Drawable

                   返回值

调用你设置修改的上下文菜单

 

public abstract ContextMenu setHeaderIcon (int iconRes)

设置上下文菜单头图标为指定的资源id

参数

iconRes   你要使用的图标资源的目录

(译者注:把图标放入res/drawable/ 目录下,R文件会自动生成对应项。设置方法如menu.setHeaderIcon(R.drawable.webtext)

这个上下文菜单头是没有设置图标的

               

这个上下文菜单头是设置了图标的 

 

返回值

调用你设置修改过的上下文菜单

 

 

public abstract ContextMenu setHeaderTitle (int titleRes)

通过资源标识符为上下文菜单头的标题栏设置文字。(译者注:需要在res/string中先设置一段你需要的文字,如:<string name="titletest">这是一段测试文字</string>

然后通过R文件索引到这段文字,menu.setHeaderTitle(R.string.titletest)

参数    

titleRes   所需文字资源的索引

                   返回值

调用你设置修改过的上下文菜单

 

public abstract ContextMenu setHeaderTitle (CharSequence title)

设置上下文菜单的标题,显示在标题栏

参数

title          标题要显示的文字

                   返回值

调用你设置修改过的上下文菜单

 

 

public abstract ContextMenu setHeaderView (View view)

设置View 到上下文菜单头上。将替代上下文菜单头的图标和标题(或者替代你之前设置的headerView

参数

view         上下文菜单头要使用的 View

                   返回值

调用你设置修改过的上下文菜单内容

 

补充

         文章精选:

                   Android 的上下文菜单: Context Menu

                   androidContextMenu

                   android 上下文菜单Context Menu

代码示例:

Test_Contextmenu.java

public class Test_Contextmenu extends Activity {

   

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        TextView txt1 = (TextView) this.findViewById(R.id.txt1);

        this.registerForContextMenu(txt1);

        TextView txt2 = (TextView) this.findViewById(R.id.txt2);

        this.registerForContextMenu(txt2);

    } 

    // 重写 onCreateContextMenu 用以创建上下文菜单

     @Override

    public void onCreateContextMenu(ContextMenu menu, View v,

        ContextMenuInfo menuInfo){

    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("菜单名称") - 用来添加子菜单。子菜单其实就是一个特殊的菜单

        SubMenu sub1 = 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);

        SubMenu sub2 = 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

<?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"

    >

<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

<?xml version="1.0" encoding="utf-8"?>

<resources>

    <string name="hello">Hello World, Test_Contextmenu!</string>

    <string name="app_name">Test_Contextmenu</string>

    <string name="titletest">这是一段测试文字</string>

</resources>


上一篇: 下一篇: