菜单

菜单的官方文档:
https://developer.android.google.cn/guide/topics/ui/menus.html

Android 操作系统中有三种基本的菜单:选项菜单(Options Menu)、上下文菜单(ContextMenu)和弹出菜单(PopupMenu)。对于所有的菜单资源,应该使用 XML 菜单资源定义菜单及其所有项。
定义菜单的步骤如下:在 res/menu/ 目录内创建一个 XML 文件,并使用

元素构建菜单。

选项菜单的创建

要为 Activity 指定选项菜单,要重写 onCreateOptionsMenu() 方法。

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.option_menu, menu);
        return true;
    }

处理点击事件
当用户从选项菜单中选择项目时,系统将调用 Activity 的 onOptionsItemSelected() 方法。

 @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.item1:
                Toast.makeText(this,"one",Toast.LENGTH_SHORT).show();
                return true;
            case R.id.item2:
                Toast.makeText(this,"two",Toast.LENGTH_SHORT).show();
                return true;
            case R.id.item3:
                Toast.makeText(this,"three",Toast.LENGTH_SHORT).show();
                return true;
            default:
                return super.onOptionsItemSelected(item);    
        }
共用菜单的处理方法.png

上下文菜单的创建

需要在创建菜单的 Activity 或 Fragment 中重写如下方法:

@Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.context_menu,menu);
    }
    @Override
    public boolean onContextItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.item4:
                Toast.makeText(this,"one",Toast.LENGTH_SHORT).show();
                return true;
            case R.id.item5:
                Toast.makeText(this,"two",Toast.LENGTH_SHORT).show();
                return true;
            case R.id.item6:
                Toast.makeText(this,"three",Toast.LENGTH_SHORT).show();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

为要绑定上下文菜单的空间添加长按响应属性:

android:longClickable="true"

并使用registerForContextMenu()方法注册

创建弹出菜单

Activity 可按照如下方式显示出菜单:

   public void showPopup(View view) {
        PopupMenu popupMenu = new PopupMenu(this,view);
        MenuInflater inflater = popupMenu.getMenuInflater();
        inflater.inflate(R.menu.popup_menu,popupMenu.getMenu());
        popupMenu.show();
        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.item7:
                        Toast.makeText(MainActivity.this, "one", Toast.LENGTH_SHORT).show();
                        return true;
                    case R.id.item8:
                        Toast.makeText(MainActivity.this, "two", Toast.LENGTH_SHORT).show();
                        return true;
                    case R.id.item9:
                        Toast.makeText(MainActivity.this, "three", Toast.LENGTH_SHORT).show();
                        return true;
                    default:
                        return false;
                }
            }
        });
    }

你可能感兴趣的:(菜单)