Android中Option menu和Context menu的使用(十六)
来源:互联网 发布:锤子手机usb网络共享 编辑:程序博客网 时间:2024/06/10 23:35
Menu由两种形式,Option menu和Context menu。前者是按下设备的Menu硬按钮弹出,后者是长按widget弹出。
1、Option Menu:当我们按下Menu的硬件按钮时,Option Menu将被触发显示,最多可以显示6个选项的icon菜单,
如果选项多于6个,第6个选项显示为“More“,点击可以进入扩展菜单。
步骤1:创建Menu
步骤2:Menu触发比较简单,Activity在Memu后会触发onOptionsItemSelected()进行处理。
步骤3:每次显示menu时根据实际的情况进行适配
步骤4:子菜单,Android支持二级菜单,但是不支持三级等多级菜单。子菜单设置如下,在onCreateOptionsMenu(),
2、Context Menu:Context Menu是用户手指长按某个View触发的菜单。
步骤1:为某个view注册ContextMenu,例如在我们的例子中,在onCreate()中对整个ListView进行处理:
步骤2:创建ContextMenu,通过Override onCreateContextMenu()来创建Context Menu。
步骤3:点击菜单触发函数,触发onContextItemSelected()。
代码片段:
package com.itarchy.menu;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;/** * * @author:itarchy * @E-mail:itarchy@163.com * @data:2015-11-19上午10:26:37 * @version: * @Des: */public class HomeActivity extends Activity {private Button btn_op;private Button btn_Ct;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);btn_op = (Button) this.findViewById(R.id.btn_op);btn_op.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {startActivity(new Intent(HomeActivity.this,OptionMenuActivity.class));}});btn_Ct = (Button) this.findViewById(R.id.btn_ct);btn_Ct.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {startActivity(new Intent(HomeActivity.this,ContextMenuActivity.class));}});}}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".OptionMenuActivity" > <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:text="菜单" android:textSize="30sp" /> <Button android:id="@+id/btn_op" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/title" android:text="选项菜单和子菜单" android:textSize="20sp" /> <Button android:id="@+id/btn_ct" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/btn_op" android:text="上下文菜单" android:textSize="20sp" /></RelativeLayout>
package com.itarchy.menu;import android.app.Activity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.SubMenu;import android.widget.Toast;public class OptionMenuActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.op_main);}// 创建选菜单@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// 1、方式一、通过一个MenuInflater把在XML布局文件中定义好的菜单样式应用在菜单上/* getMenuInflater().inflate(R.menu.main, menu); */// 2、方式二:// 直接在Java代码中使用add()方法逐个添加菜单项menu.add(Menu.NONE, 0, Menu.NONE, "设置").setIcon(R.drawable.qq);menu.add(Menu.NONE, 1, Menu.NONE, "添加").setIcon(R.drawable.qq_kong);// 给第三项添加子菜单SubMenu subMenu = menu.addSubMenu(Menu.NONE, 2, Menu.NONE, "子菜单1").setIcon(R.drawable.qq);subMenu.setHeaderIcon(R.drawable.qq_kong);subMenu.setHeaderTitle("子菜单标题");subMenu.add(Menu.NONE, 112, Menu.NONE, "子菜单1");subMenu.add(Menu.NONE, 113, Menu.NONE, "子菜单2");subMenu.add(Menu.NONE, 114, Menu.NONE, "子菜单3");subMenu.add(Menu.NONE, 115, Menu.NONE, "子菜单4");menu.add(Menu.NONE, 3, Menu.NONE, "图库").setIcon(R.drawable.qq);menu.add(Menu.NONE, 4, Menu.NONE, "相机").setIcon(R.drawable.qq_kong);menu.add(Menu.NONE, 5, Menu.NONE, "音乐").setIcon(R.drawable.qq);menu.add(Menu.NONE, 6, Menu.NONE, "蓝牙").setIcon(R.drawable.qq_kong);menu.add(Menu.NONE, 7, Menu.NONE, "个人中心");return true;}// 点击菜单的items时执行@Overridepublic boolean onOptionsItemSelected(MenuItem item) {Toast.makeText(getApplication(),item.getTitle() + "," + (item.getItemId() + ""), 100).show();switch (item.getItemId()) {case 0:// 处理"设置"break;case 1:// 处理"添加"break;}return super.onOptionsItemSelected(item);}}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".OptionMenuActivity" > <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:textSize="30sp" android:text="选项菜单和子菜单" /></RelativeLayout>
package com.itarchy.menu;import android.app.Activity;import android.os.Bundle;import android.view.ContextMenu;import android.view.ContextMenu.ContextMenuInfo;import android.view.Menu;import android.view.MenuItem;import android.view.SubMenu;import android.view.View;import android.widget.TextView;import android.widget.Toast;public class ContextMenuActivity extends Activity {private TextView tvMenu;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.ct_main);tvMenu = (TextView) this.findViewById(R.id.title);this.registerForContextMenu(tvMenu);}// 创建上下文菜单@Overridepublic void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {menu.add(Menu.NONE, 0, Menu.NONE, "设置").setIcon(R.drawable.qq);menu.add(Menu.NONE, 1, Menu.NONE, "添加").setIcon(R.drawable.qq_kong);// 给第三项添加子菜单SubMenu subMenu = menu.addSubMenu(Menu.NONE, 2, Menu.NONE, "子菜单1").setIcon(R.drawable.qq);subMenu.setHeaderIcon(R.drawable.qq_kong);subMenu.setHeaderTitle("子菜单标题");subMenu.add(Menu.NONE, 112, Menu.NONE, "子菜单1");subMenu.add(Menu.NONE, 113, Menu.NONE, "子菜单2");subMenu.add(Menu.NONE, 114, Menu.NONE, "子菜单3");subMenu.add(Menu.NONE, 115, Menu.NONE, "子菜单4");menu.add(Menu.NONE, 3, Menu.NONE, "图库").setIcon(R.drawable.qq);menu.add(Menu.NONE, 4, Menu.NONE, "相机").setIcon(R.drawable.qq_kong);menu.add(Menu.NONE, 5, Menu.NONE, "音乐").setIcon(R.drawable.qq);menu.add(Menu.NONE, 6, Menu.NONE, "蓝牙").setIcon(R.drawable.qq_kong);menu.add(Menu.NONE, 7, Menu.NONE, "个人中心");super.onCreateContextMenu(menu, v, menuInfo);}@Overridepublic boolean onContextItemSelected(MenuItem item) {// TODO Auto-generated method stubToast.makeText(getApplication(),item.getTitle() + "," + (item.getItemId() + ""), 100).show();switch (item.getItemId()) {case 0:// 处理"设置"break;case 1:// 处理"添加"break;}return super.onContextItemSelected(item);}}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".OptionMenuActivity" > <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:textSize="30sp" android:text="上下文菜单" /></RelativeLayout>
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.itarchy.menu" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.itarchy.menu.HomeActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="com.itarchy.menu.OptionMenuActivity" /> <activity android:name="com.itarchy.menu.ContextMenuActivity" /> </application></manifest>
0 0
- Android中Option menu和Context menu的使用(十六)
- Option Menu && Context Menu的使用
- android 通过 button 弹出 option menu 和 context menu
- Android 菜单, option menu 和 context menu (右键菜单)
- 【android】在fragment中使用option menu
- Android Context Menu和Options Menu菜单的区别
- Android编程 - Option Menu选项菜单&& Context Menu上下文菜单基本使用
- android中menu的使用
- Android中Menu的使用
- android 中Menu的使用
- android中Menu的使用
- Android中Menu的使用
- android的Context Menu菜单
- Android的context menu组件
- Context-Menu.Android库的使用(修改显示位置)
- android menu的使用(Menu无法弹出问题)
- android Context Menu
- Android Menu的使用
- EXECUTORSERVICE线程池讲解
- Java 动态代理
- 利用ViewHolder优化BaseAdapter(ListView)
- C#动态库在Unity3D 4.5版本上可以编译通过,但在5.2版本上编译不通过的问题
- Spring框架IOC与AOP简单应用浅解-适合初入SSH框架学习
- Android中Option menu和Context menu的使用(十六)
- C/C++参数传递的3种方式
- Linux下实用的查看内存和多核CPU状态命令
- unity3d 陀螺仪代码
- EventBus使用详解(二)——EventBus使用进阶
- Android之蓝牙开发浅析
- Java字节码重写
- ORA-00060: Deadlock detected
- delphi property 实例(包含数组属性)