android之三大菜单(子菜单,选项菜单,上下文菜单)

今天班级同学聚餐,真的是喝的有点多,本来计划今天要完成这篇博客。有点喝多了,写的不好请见谅。想仔细了解的话,看官网说的,是中文的。
https://developer.android.com/guide/topics/ui/menus.html

选项菜单

> 只要重写onCreateOptionsMenu()方法即可

MainActivity.java代码如下:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //四个参数的含义。1,group的id,2,item的id,3,是否排序,4,将要显示的内容
        menu.add(0,1,0,"菜单一");
        menu.add(0,2,0,"菜单二");
        menu.add(0,3,0,"菜单三");
        menu.add(0,4,0,"菜单四");

        return true;
    }
}

运行效果

android之三大菜单(子菜单,选项菜单,上下文菜单)_第1张图片

接下来为菜单添加点击事件,需要重写onOptionsItemSelected()方法。

MainActivity.java代码如下:


import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //四个参数的含义。1,group的id,2,item的id,3,是否排序,4,将要显示的内容
        menu.add(0,1,0,"菜单一");
        menu.add(0,2,0,"菜单二");
        menu.add(0,3,0,"菜单三");
        menu.add(0,4,0,"菜单四");

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case 1:
                Toast.makeText(MainActivity.this,"菜单一",Toast.LENGTH_SHORT).show();
                break;
            case 2:
                Toast.makeText(MainActivity.this,"菜单二",Toast.LENGTH_SHORT).show();
                break;
            case 3:
                Toast.makeText(MainActivity.this,"菜单三",Toast.LENGTH_SHORT).show();
                break;
            case 4:
                Toast.makeText(MainActivity.this,"菜单四",Toast.LENGTH_SHORT).show();
                break;
        }
        return true;
    }
}

运行效果

android之三大菜单(子菜单,选项菜单,上下文菜单)_第2张图片

子菜单

只要在onCreateOptionsMenu()方法中添加
SubMenu sub=menu.addSubMenu(“子菜单”);
sub.add(0,5,0,”子菜单一”);
sub.add(0,6,0,”子菜单二”);
sub.add(0,7,0,”子菜单三”);
然后再为子菜单添加点击事件。

MainActivity.java全部代码如下

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //四个参数的含义。1,group的id,2,item的id,3,是否排序,4,将要显示的内容
        menu.add(0,1,0,"菜单一");
        menu.add(0,2,0,"菜单二");
        menu.add(0,3,0,"菜单三");
        menu.add(0,4,0,"菜单四");
        SubMenu sub=menu.addSubMenu("子菜单");
        sub.add(0,5,0,"子菜单一");
        sub.add(0,6,0,"子菜单二");
        sub.add(0,7,0,"子菜单三");
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case 1:
                Toast.makeText(MainActivity.this,"菜单一",Toast.LENGTH_SHORT).show();
                break;
            case 2:
                Toast.makeText(MainActivity.this,"菜单二",Toast.LENGTH_SHORT).show();
                break;
            case 3:
                Toast.makeText(MainActivity.this,"菜单三",Toast.LENGTH_SHORT).show();
                break;
            case 4:
                Toast.makeText(MainActivity.this,"菜单四",Toast.LENGTH_SHORT).show();
                break;
            case 5:
                Toast.makeText(MainActivity.this,"子菜单一",Toast.LENGTH_SHORT).show();
                break;
            case 6:
                Toast.makeText(MainActivity.this,"子菜单二",Toast.LENGTH_SHORT).show();
                break;
            case 7:
                Toast.makeText(MainActivity.this,"子菜单三",Toast.LENGTH_SHORT).show();
                break;
        }
        return true;
    }
}

上下文菜单

只要重写这个方法onCreateContextMenu()如果在为它添加点击事件,就需要再重写onContextItemSelected()方法

MainActivity.java代码如下:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ContextMenu;
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 MainActivity extends AppCompatActivity {
    private TextView mTv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTv = (TextView) findViewById(R.id.tv);
        registerForContextMenu(mTv);//为mTv注册事件
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //四个参数的含义。1,group的id,2,item的id,3,是否排序,4,将要显示的内容
        menu.add(0, 1, 0, "菜单一");
        menu.add(0, 2, 0, "菜单二");
        menu.add(0, 3, 0, "菜单三");
        menu.add(0, 4, 0, "菜单四");
        SubMenu sub = menu.addSubMenu("子菜单");
        sub.add(0, 5, 0, "子菜单一");
        sub.add(0, 6, 0, "子菜单二");
        sub.add(0, 7, 0, "子菜单三");
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case 1:
                Toast.makeText(MainActivity.this, "菜单一", Toast.LENGTH_SHORT).show();
                break;
            case 2:
                Toast.makeText(MainActivity.this, "菜单二", Toast.LENGTH_SHORT).show();
                break;
            case 3:
                Toast.makeText(MainActivity.this, "菜单三", Toast.LENGTH_SHORT).show();
                break;
            case 4:
                Toast.makeText(MainActivity.this, "菜单四", Toast.LENGTH_SHORT).show();
                break;
            case 5:
                Toast.makeText(MainActivity.this, "子菜单一", Toast.LENGTH_SHORT).show();
                break;
            case 6:
                Toast.makeText(MainActivity.this, "子菜单二", Toast.LENGTH_SHORT).show();
                break;
            case 7:
                Toast.makeText(MainActivity.this, "子菜单三", Toast.LENGTH_SHORT).show();
                break;
        }
        return true;
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        if (v == mTv) {
            menu.add(0, 1, 0, "上下文菜单一");
            menu.add(0, 2, 0, "上下文菜单二");
            menu.add(0, 3, 0, "上下文菜单三");

        }

        super.onCreateContextMenu(menu, v, menuInfo);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case 1:
                Toast.makeText(MainActivity.this, "上下文菜单一", Toast.LENGTH_SHORT).show();
                break;
            case 2:
                Toast.makeText(MainActivity.this, "上下文菜单二", Toast.LENGTH_SHORT).show();
                break;
            case 3:
                Toast.makeText(MainActivity.this, "上下文菜单三", Toast.LENGTH_SHORT).show();
                break;

        }
        return true;
    }
}

需要长按Textview才行,运行结果,

android之三大菜单(子菜单,选项菜单,上下文菜单)_第3张图片

选项菜单,子菜单,上下文菜单基本使用方法基本就演示完了。
其实也可以写布局文件,直接加载布局文件也行,下面我们就演示下吧。


<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <group android:id="@+id/group">
        <item
            android:id="group_one"
            android:title="选项菜单一">

        item>
        <item
            android:id="group_two"
            android:title="选项菜单二">

        item>
        <item
            android:id="group_three"
            android:title="选项菜单三">

        item>
        <item
            android:id="group_four"
            android:title="选项菜单四">
            <menu>
                <item
                    android:id="child_one"
                    android:title="子菜单一">
                item>
                <item
                    android:id="child_two"
                    android:title="子菜单二">
                item>
                <item
                    android:id="child_three"
                    android:title="子菜单三">
                item>
            menu>
        item>
    group>
menu>

MainActivity.java代码如下:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.LayoutInflater;
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 MainActivity extends AppCompatActivity {
    private TextView mTv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTv = (TextView) findViewById(R.id.tv);
        registerForContextMenu(mTv);//为mTv注册事件
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //四个参数的含义。1,group的id,2,item的id,3,是否排序,4,将要显示的内容
//        menu.add(0, 1, 0, "菜单一");
//        menu.add(0, 2, 0, "菜单二");
//        menu.add(0, 3, 0, "菜单三");
//        menu.add(0, 4, 0, "菜单四");
//        SubMenu sub = menu.addSubMenu("子菜单");
//        sub.add(0, 5, 0, "子菜单一");
//        sub.add(0, 6, 0, "子菜单二");
//        sub.add(0, 7, 0, "子菜单三");

        getMenuInflater().inflate(R.menu.item_menu,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.group_one:
                Toast.makeText(MainActivity.this, "菜单一", Toast.LENGTH_SHORT).show();
                break;
            case R.id.group_two:
                Toast.makeText(MainActivity.this, "菜单二", Toast.LENGTH_SHORT).show();
                break;
            case R.id.group_three:
                Toast.makeText(MainActivity.this, "菜单三", Toast.LENGTH_SHORT).show();
                break;
            case R.id.group_four:
                Toast.makeText(MainActivity.this, "菜单四", Toast.LENGTH_SHORT).show();
                break;
            case R.id.child_one:
                Toast.makeText(MainActivity.this, "子菜单一", Toast.LENGTH_SHORT).show();
                break;
            case R.id.child_two:
                Toast.makeText(MainActivity.this, "子菜单二", Toast.LENGTH_SHORT).show();
                break;
            case R.id.child_three:
                Toast.makeText(MainActivity.this, "子菜单三", Toast.LENGTH_SHORT).show();
                break;
        }
        return true;
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        if (v == mTv) {
            menu.add(0, 1, 0, "上下文菜单一");
            menu.add(0, 2, 0, "上下文菜单二");
            menu.add(0, 3, 0, "上下文菜单三");

        }

        super.onCreateContextMenu(menu, v, menuInfo);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case 1:
                Toast.makeText(MainActivity.this, "上下文菜单一", Toast.LENGTH_SHORT).show();
                break;
            case 2:
                Toast.makeText(MainActivity.this, "上下文菜单二", Toast.LENGTH_SHORT).show();
                break;
            case 3:
                Toast.makeText(MainActivity.this, "上下文菜单三", Toast.LENGTH_SHORT).show();
                break;

        }
        return true;
    }
}

运行效果还是跟前面的选项菜单一样。只是数据来自menu中

运行结果:

android之三大菜单(子菜单,选项菜单,上下文菜单)_第4张图片

总结

其实这三种菜单不难,只要按照写一遍就会了。

源码下载

https://github.com/songshuilin/AndroidForBlog/tree/master/androidformenu

你可能感兴趣的:(android一点一滴)