首页 > 代码库 > android各种菜单使用介绍

android各种菜单使用介绍

Android菜单的有这几种:

  1,OptionMenue:选项菜单
  2,contextMenu:上下文菜单
  3,SubMenu子菜单

其中,OptionMenue与contextMenu的区别(OptionMenue与SubMenu区别较小):
  1,OptionMenue对应的四activity,一个activity只能拥有一个选项菜单
  2,contextMenu对于的是View,每一个View都可以设置上下文菜单
  3,一般情况下contextMenu常用于ListView或者GridView;

下面分别进行详细的介绍:

第一:OptionMenue:选项菜单,

  1):菜单的实现:

    在activity中实现方法:onCreateOptionsMenu()

    1.1:设置菜单项可用代码动态设置menuadd();
        1.2:通过xml设置,getMenuInflater().inflate

  2):监听的实现:

    在activity中实现方法:onCreateOptionsMenu()

 

    示例如下:

    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.main, menu);        return true;    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {        int id = item.getItemId();        switch (id) {        case R.id.action_menu_item1:            //设置跳转界面            Intent intent = new Intent(MainActivity.this, SettingActivity.class);            item.setIntent(intent);            //Toast.makeText(this, "点击了菜单1", 0).show();            break;        case R.id.action_menu_item2:            Toast.makeText(this, "点击了菜单2", 0).show();            break;        }                return super.onOptionsItemSelected(item);    }

其中main.xml的实现如下:

<menu xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    tools:context="com.example.optionmenu.MainActivity" >    <item        android:id="@+id/action_menu_item1"        android:orderInCategory="100"        android:showAsAction="never"        android:title="设置"/>    <item        android:id="@+id/action_menu_item2"        android:orderInCategory="100"        android:showAsAction="never"        android:title="选项"/></menu>

 

第二:contextMenu:上下文菜单(windows下右键的弹出,Android下针对文件长按):

构成:图标,标题,菜单项,监听

创建方法:
  1,在View注册上下文菜单registerForContextMenu()
  2,添加上下文菜单内容onCreateContextMenu()
    2.1:可以通过代码动态添加
    2.2:可以加载xml文件夹中的菜单项
  3,设置菜单点击后的响应事件onContenxtItemSelected();

示例:mainActivity的实现如下:

public class MainActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        showListView();    }  //设置展示的列表    private void showListView(){        ListView listView = (ListView) findViewById(R.id.listview);        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,                 android.R.layout.simple_list_item_1, getData());        listView.setAdapter(adapter);        this.registerForContextMenu(listView);//设置菜单选项    }
   //为列表设置数据
private ArrayList<String> getData(){ ArrayList<String> list = new ArrayList<String>(); for(int i=0;i<15;i++){ list.add("列表"+i); } return list; } @Overridepublic void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); //设置菜单显示内容 menu.setHeaderTitle("操作");
  //方法二:通过xml进行选项的设置 menu.setHeaderIcon(R.drawable.ic_launcher);
  //方法一:通过add动态的添加
// menu.add(1, 1, 1, "复制");// menu.add(1, 2, 1, "粘贴");// menu.add(1, 3, 1, "剪切");// menu.add(1, 4, 1, "重命名"); MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.main, menu);} @Overridepublic boolean onContextItemSelected(MenuItem item) { switch (item.getItemId()) { case 1: Toast.makeText(this, "点击复制", 0).show(); break; case 2: Toast.makeText(this, "点击粘贴", 0).show(); break; case 3: Toast.makeText(this, "点击剪切", 0).show(); break; case 4: Toast.makeText(this, "点击重命名", 0).show(); break; } return super.onContextItemSelected(item);} }

 

第三:SubMenu子菜单:

这个实现起来和OptionMenue差不多,所需要实现的方法也是一致的,只是需要在onCreateOptionsMenu()方法中定义SubMenu

对象,

具体示例如下:

 

public class MainActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        // getMenuInflater().inflate(R.menu.main, menu);        SubMenu file = menu.addSubMenu("文件");        SubMenu edit = menu.addSubMenu("编辑");        file.add(1, 1, 1, "新建");        file.add(1, 2, 1, "打开");        file.add(1, 3, 1, "保存");        file.setHeaderTitle("文件操作");        file.setHeaderIcon(R.drawable.ic_launcher);        edit.add(2, 1, 1, "复制");        edit.add(2, 2, 1, "粘贴");        edit.add(2, 3, 1, "剪切");        edit.add(2, 4, 1, "重命名");        edit.setHeaderTitle("编辑操作");        edit.setHeaderIcon(R.drawable.ic_launcher);        return true;    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {        // Handle action bar item clicks here. The action bar will        // automatically handle clicks on the Home/Up button, so long        // as you specify a parent activity in AndroidManifest.xml.        if(item.getGroupId()==1){            switch (item.getItemId()) {            case 1:                Toast.makeText(this, "点击新建", 0).show();                 break;            case 2:                Toast.makeText(this, "点击打开", 0).show();                 break;            case 3:                Toast.makeText(this, "点击保存", 0).show();                 break;            }        }else if(item.getGroupId()==2){            switch (item.getItemId()) {             case 1:                Toast.makeText(this, "点击复制", 0).show();                break;             case 2:                Toast.makeText(this, "点击粘贴", 0).show();                break;             case 3:                Toast.makeText(this, "点击剪切", 0).show();                break;            case 4:                Toast.makeText(this, "点击重命名", 0).show();                 break;            }        }        return super.onOptionsItemSelected(item);    }}

 

作者:Darren

微博:@IT_攻城师

github:@Darren90

出处:http://www.cnblogs.com/fengtengfei/

 

android各种菜单使用介绍