android中菜单分为三种,分别是,options menu 选项菜单,sub menu 子菜单,context menu 上下文菜单。下面分别举例演示这三种菜单:
新建一个android project,
options menu,第一种方法,通过xml定义选项菜单,在res文件夹中新建一个名为menu的文件夹,在其内新建一个options_menu.xml的文件:
- <?xml version="1.0" encoding="utf-8"?>
- <menu xmlns:android="http://schemas.android.com/apk/res/android" >
- <item
- android:id="@+id/newFile"
- android:icon="@drawable/ic_launcher"
- android:title="@string/newFile"
- />
- <!-- android:icon 设置菜单选项的图标
- android:title 设置菜单选项的标题 -->
- <item
- android:id="@+id/exit"
- android:icon="@drawable/ic_launcher"
- android:title="@string/exit"
- />
- </menu>
在MenuTestActivity1中:
- package cn.sword.activity;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.MenuInflater;
- import android.view.MenuItem;
- import android.view.MenuItem.OnMenuItemClickListener;
- import android.widget.Toast;
- public class MenuTestActivity1 extends Activity implements OnMenuItemClickListener{
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- MenuInflater inflater = this.getMenuInflater();
- inflater.inflate(R.menu.options_menu, menu);
- //第一种响应菜单点击事件的方法
- //为每一个菜单选项添加监听事件
- //在覆盖的onMenuItemClick()方法中判断并触发监听事件
- for(int i=0;i<menu.size();i++){
- menu.getItem(i).setOnMenuItemClickListener((OnMenuItemClickListener) this);
- }
- return super.onCreateOptionsMenu(menu);
- }
- /*第二种响应菜单点击事件的方法 覆盖onOptionsItemSelected()方法
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- switch(item.getItemId()){
- case R.id.newFile:
- Toast.makeText(this,"new File", Toast.LENGTH_LONG).show();
- break;
- case R.id.exit:
- this.finish();//退出程序
- break;
- }
- return super.onOptionsItemSelected(item);
- }*/
- public boolean onMenuItemClick(MenuItem item){
- switch(item.getItemId()){
- case R.id.newFile:
- Toast.makeText(this, "new File", Toast.LENGTH_LONG).show();
- break;
- case R.id.exit:
- this.finish();
- }
- return false;
- }
- }
在模拟器中点击Menu键效果:
options menu,第二种方法,动态添加删除菜单
在MenuTestActivity2中:
- package cn.class3g.activity;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.MenuInflater;
- import android.view.MenuItem;
- import android.widget.Toast;
- public class MenuTestActivity2 extends Activity {
- <span style="white-space:pre"> </span>//定义菜单项
- private static final int SAVE=1;
- private static final int SAVESAVE_AS = SAVE+1;
- private static final int DELETE = SAVE+2;
- //声明一个menu对象
- Menu menu = null;
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- }
- public boolean onCreateOptionsMenu(Menu menu) {
- MenuInflater inflater = this.getMenuInflater();
- inflater.inflate(R.menu.options_menu, menu);
- //向菜单中添加菜单项
- menu.add(1, SAVE, 1, "保存");
- menu.add(1,SAVE_AS, 2, "另存为");
- menu.add(1,DELETE, 3, "删除保存");
- //设置菜单项的图标
- menu.findItem(SAVE).setIcon(R.drawable.ic_launcher);
- this.menu = menu;
- return true;
- }
- <span style="white-space:pre"> </span>//动态的修改菜单title
- int i=1;
- public boolean onPrepareOptionsMenu(Menu menu) {
- MenuItem item = menu.findItem(SAVE_AS);
- item.setTitle(item.getTitle() + "" + i);
- i++;
- return super.onPrepareOptionsMenu(menu);
- }
- lt;span style="white-space:pre"> </span>//响应菜单单击事件
- public boolean onOptionsItemSelected(MenuItem item) {
- switch (item.getItemId()) {
- case R.id.newFile:
- Toast.makeText(this, "new file", Toast.LENGTH_LONG).show();//利用Toast弹出文本信息
- break;
- case R.id.exit:
- // Toast.makeText(this, "exit", Toast.LENGTH_LONG).show();
- this.finish();//关闭Activity
- break;
- case DELETE:<pre name="code" class="html"><span style="white-space:pre"> </span>//menu.removeGroup(1);//删除一组菜单
sub menu 子菜单
利用xml定义菜单参考options menu。
动态的添加子菜单:
在MenuTestActivity3中:
- package cn.sword.activity;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.MenuInflater;
- import android.view.MenuItem;
- import android.view.SubMenu;
- import android.widget.Toast;
- public class MenuTestActivity3 extends Activity {
- <span style="white-space:pre"> </span>//定义菜单项
- private static final int SUBMENU = 10;
- private static final int SUBITEM1 = SUBMENU+1;
- private static final int SUBITEM2 = SUBMENU+2;
- private static final int SUBITEM3 = SUBMENU+3;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- this.setContentView(R.layout.main);
- }
- public boolean onCreateOptionsMenu(Menu menu){
- MenuInflater inflater = this.getMenuInflater();
- inflater.inflate(R.menu.sub_menu, menu);
- //添加子菜单
- SubMenu subMenu = menu.addSubMenu(1,SUBMENU,1,"子菜单");
- subMenu.add(20,SUBITEM1,1,"subitem1");
- subMenu.add(20,SUBITEM2,2,"subitem2");
- subMenu.add(30,SUBITEM3,3,"subitem3");
- subMenu.setGroupCheckable(20, true, false);
- //第二个参数是否可选
- //当第三个参数为true时为这组菜单为互斥(单选)菜单
- //当第三个参数为false时为组菜单为复选菜单
- MenuItem item = subMenu.getItem(0);
- //item.setCheckable(false);//设置该项菜单为单选或复选
- item.setChecked(true);//设置菜单的默认选项
- //subMenu.getItem(0).setCheckable(true);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- switch(item.getItemId()){
- case R.id.menuitem1:
- Toast.makeText(this,"menuitem1", Toast.LENGTH_LONG).show();
- break;
- case R.id.menuitem2:
- Toast.makeText(this,"menuitem2", Toast.LENGTH_LONG).show();
- break;
- case R.id.subitem1:
- Toast.makeText(this,"subitem1", Toast.LENGTH_LONG).show();
- break;
- case R.id.subitem2:
- Toast.makeText(this,"subitem2", Toast.LENGTH_LONG).show();
- break;
- case SUBITEM1:
- Toast.makeText(this,"SUBITEM1", Toast.LENGTH_LONG).show();
- break;
- case SUBITEM2:
- Toast.makeText(this,"SUBITEM2", Toast.LENGTH_LONG).show();
- //this.finish();//退出程序
- break;
- }
- return super.onOptionsItemSelected(item);
- }
- }
在模拟器中点击Menu键:
单击子菜单:
context menu 上下文菜单:context menu需要绑定在TextView上并在其上按2s后显示。
在MenuTestActivity4中:
- package cn.sword.activity;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.ContextMenu;
- import android.view.ContextMenu.ContextMenuInfo;
- import android.view.MenuInflater;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.TextView;
- import android.widget.Toast;
- public class MenuTestActivity4 extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- this.setContentView(R.layout.main);
- //将上下文菜单绑定到TextView上
- TextView tv = (TextView) this.findViewById(R.id.textView);
- this.registerForContextMenu(tv);
- }
- @Override
- public boolean onContextItemSelected(MenuItem item) {
- switch(item.getItemId()){
- case R.id.subitem1:
- Toast.makeText(this, "subitem1",Toast.LENGTH_LONG).show();
- break;
- case R.id.subitem2:
- Toast.makeText(this, "subitem2",Toast.LENGTH_LONG).show();
- break;
- }
- return super.onContextItemSelected(item);
- }
- //每次上下文菜单显示都会调用
- public void onCreateContextMenu(ContextMenu menu, View v,
- ContextMenuInfo menuInfo) {
- MenuInflater inflater = this.getMenuInflater();
- inflater.inflate(R.menu.sub_menu, menu);
- super.onCreateContextMenu(menu, v, menuInfo);
- }
- }
运行模拟器,点击文本内容2s后: