阅读《Android 从入门到精通》(26)——上下文菜单
Posted SweetLoverFT
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了阅读《Android 从入门到精通》(26)——上下文菜单相关的知识,希望对你有一定的参考价值。
上下文菜单(ContextMenu)
上下文菜单不同于 Windows 菜单,需要长按才能出现子菜单。
ContextMenu 类方法
ContextMenu 示例
完整工程:http://download.csdn.net/detail/sweetloveft/9435611
1.MainActivity.java
package com.sweetlover.activity;
import com.sweetlover.contextmenu.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.SubMenu;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity
private TextView tv1 = null;
private TextView tv2 = null;
@Override
protected void onCreate(Bundle savedInstanceState)
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1 = (TextView)findViewById(R.id.textView1);
tv2 = (TextView)findViewById(R.id.textView2);
// 将控件注册到上下文之中
registerForContextMenu(tv1);
registerForContextMenu(tv2);
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo)
// TODO Auto-generated method stub
super.onCreateContextMenu(menu, v, menuInfo);
if (v == tv1)
menu.setHeaderIcon(R.drawable.miku);
menu.setHeaderTitle("我的菜单");
menu.add(1, 0, 0, "菜单1");
menu.add(1, 1, 1, "菜单2");
for (int i = 3; i < 8; i++)
menu.add(1, 1, 2, "菜单" + i);
else if (v == tv2)
SubMenu[] subMenu = new SubMenu[6];
subMenu[0] = menu.addSubMenu("二级菜单1");
subMenu[0].setHeaderIcon(R.drawable.miku);
subMenu[0].add(0, 0, 0, "二级菜单1/菜单1");
subMenu[0].add(0, 1, 1, "二级彩单1/菜单2");
subMenu[0].setGroupCheckable(1, true, true); // 设置整个组可选
subMenu[1] = menu.addSubMenu("二级菜单2");
subMenu[1].setHeaderIcon(R.drawable.miku);
subMenu[1].add(1, 0, 0, "二级菜单2/菜单1");
subMenu[1].add(1, 1, 1, "二级彩单2/菜单2");
subMenu[1].setGroupCheckable(1, true, true); // 设置整个组可选
subMenu[2] = menu.addSubMenu("二级菜单3");
subMenu[2].setHeaderIcon(R.drawable.miku);
subMenu[2].add(1, 0, 0, "二级菜单3/菜单1");
subMenu[2].add(1, 1, 1, "二级彩单3/菜单2");
subMenu[2].setGroupCheckable(1, true, true); // 设置整个组可选
subMenu[3] = menu.addSubMenu("二级菜单4");
subMenu[3].setHeaderIcon(R.drawable.miku);
subMenu[3].add(1, 0, 0, "二级菜单4/菜单1");
subMenu[3].add(1, 1, 1, "二级彩单4/菜单2");
subMenu[3].setGroupCheckable(1, true, true); // 设置整个组可选
subMenu[4] = menu.addSubMenu("二级菜单5");
subMenu[4].setHeaderIcon(R.drawable.miku);
subMenu[4].add(1, 0, 0, "二级菜单5/菜单1");
subMenu[4].add(1, 1, 1, "二级彩单5/菜单2");
subMenu[4].setGroupCheckable(1, true, true); // 设置整个组可选
subMenu[5] = menu.addSubMenu("二级菜单6");
subMenu[5].setHeaderIcon(R.drawable.miku);
subMenu[5].add(1, 0, 0, "二级菜单6/菜单1");
subMenu[5].add(1, 1, 1, "二级彩单6/菜单2");
subMenu[5].setGroupCheckable(1, true, true); // 设置整个组可选
2.activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/note"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/item1"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/item2"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
3.string.xml
<resources>
<string name="app_name">ContextMenu</string>
<string name="note">长时间按住显示菜单</string>
<string name="item1">主菜单1</string>
<string name="item2">主菜单2</string>
</resources>
以上是关于阅读《Android 从入门到精通》(26)——上下文菜单的主要内容,如果未能解决你的问题,请参考以下文章