Android中菜单的使用

来源:互联网 发布:手机淘宝我要开店 编辑:程序博客网 时间:2024/05/16 05:31

一、使用代码设置菜单

1.设置选项菜单和子菜单

实现思路:
1.重写onCreateOptionsMenu(Menu menu)方法来添加菜单项或者子菜单
2.重写onOptionsItemSelected(MenuItem item)方法来设置菜单的响应事件

  • 代码文件
public class MainActivity extends AppCompatActivity {    final int FONT_10 = 0x111;    final int FONT_12 = 0x112;    final int FONT_14 = 0x113;    final int FONT_16 = 0x114;    final int FONT_18 = 0x115;    final int PLAIN_ITEM = 0x11b;    final int FONT_RED = 0x116;    final int FONT_BLUE = 0x117;    final int FONT_GREEN = 0x118;    EditText editText;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        editText = (EditText) findViewById(R.id.editText);    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        //设置可以设置子菜单的菜单项        SubMenu fontMenu = menu.addSubMenu("字体大小");        //为菜单项设置图标        fontMenu.setIcon(R.mipmap.ic_launcher);        fontMenu.setHeaderIcon(R.mipmap.ic_launcher);        //为菜单项设置标题        fontMenu.setHeaderTitle("选择字体大小");        //添加子菜单        fontMenu.add(0, FONT_10, 0, "10号字体");        fontMenu.add(0, FONT_12, 0, "12号字体");        fontMenu.add(0, FONT_14, 0, "14号字体");        fontMenu.add(0, FONT_16, 0, "16号字体");        fontMenu.add(0, FONT_18, 0, "18号字体");        //添加一个普通子菜单项        menu.add(0, PLAIN_ITEM, 0, "普通菜单项");        MenuItem start = fontMenu.add("启动");        start.setIntent(new Intent(MainActivity.this, Other.class));        SubMenu colorMenu = menu.addSubMenu("字体颜色");        colorMenu.setIcon(R.mipmap.ic_launcher);        colorMenu.setHeaderIcon(R.mipmap.ic_launcher);        colorMenu.setHeaderTitle("选择文字颜色");        colorMenu.add(0, FONT_RED,0,"红色");        colorMenu.add(0, FONT_BLUE,0,"蓝色");        colorMenu.add(0, FONT_GREEN,0,"绿色");        return super.onCreateOptionsMenu(menu);    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {        switch (item.getItemId()) {            case FONT_10:                editText.setTextSize(10 * 2);                break;            case FONT_12:                editText.setTextSize(12 * 2);                break;            case FONT_14:                editText.setTextSize(14 * 2);                break;            case FONT_16:                editText.setTextSize(16 * 2);                break;            case FONT_18:                editText.setTextSize(18 * 2);                break;            case FONT_RED:                editText.setTextColor(Color.RED);                break;            case FONT_GREEN:                editText.setTextColor(Color.GREEN);                break;            case FONT_BLUE:                editText.setTextColor(Color.BLUE);                break;            case PLAIN_ITEM:                break;        }        return true;    }}

2.设置上下文菜单

上下文菜单可以绑定一个控件,长按该控件会弹出上下文菜单,

实现思路:
1.重写onCreateContextMenu(ContextMenu menu,View view,ContextMenuInfo menuInfo)方法
2.调用registerForContextMenu(View view)方法为组件注册上下文菜单
3.重写onContextItemSelected(MenuItem item)方法为其设置响应事件

  • 代码文件
public class MainActivity extends AppCompatActivity {    final int MENU1 = 0x111;    final int MENU2 = 0x112;    final int MENU3 = 0x113;    TextView textView;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        textView = (TextView) findViewById(R.id.textView);        //为该控件注册菜单项        registerForContextMenu(textView);    }    //设置上下文菜单    @Override    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {        menu.add(0, MENU1, 0, "红色");        menu.add(0, MENU2, 0, "绿色");        menu.add(0, MENU3, 0, "蓝色");        menu.setGroupCheckable(0,true,true);        menu.setHeaderIcon(R.mipmap.ic_launcher);        menu.setHeaderTitle("选择背景色");    }    @Override    public boolean onContextItemSelected(MenuItem item) {        switch (item.getItemId()) {            case R.id.red:                item.setChecked(true);                textView.setBackgroundColor(Color.RED);                break;            case R.id.blue:                    item.setChecked(true);                    textView.setBackgroundColor(Color.GREEN);                    break;            case R.id.green:                    item.setChecked(true);                    textView.setBackgroundColor(Color.BLUE);                    break;        }        return true;    }

二、使用XML文件定义菜单

1.设置选项菜单和子菜单

通过在XML文件中设置菜单,在代码中指定其资源文件,并为其重写事件响应方法

  • XML菜单文件
<menu xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    tools:context="top.hellowoodes.contextmenu.MainActivity">    <item        android:title="@string/font_size"        android:icon="@mipmap/ic_launcher">        <menu>            <group android:checkableBehavior="single">                <item                    android:id="@+id/font_10"                    android:title="@string/font_10" />                <item                    android:id="@+id/font_12"                    android:title="@string/font_12" />                <item                    android:id="@+id/font_14"                    android:title="@string/font_14" />                <item                    android:id="@+id/font_16"                    android:title="@string/font_16" />                <item                    android:id="@+id/font_18"                    android:title="@string/font_18" />            </group>        </menu>    </item>    <item        android:id="@+id/palin_item"        android:title="@string/plain_item" />    <item        android:title="@string/font_color"        android:icon="@mipmap/ic_launcher">        <menu>            <group>                <item                    android:id="@+id/red_font"                    android:title="@string/red_title" />                <item                    android:id="@+id/green_font"                    android:title="@string/green_title" />                <item                    android:id="@+id/blue_fone"                    android:title="@string/blue_title" />            </group>        </menu>    </item></menu>

item元素包含menu元素,menu中又可以包含item元素,表示子菜单,也可以包含group元素,代表一个菜单组,用于指定其全组属性,group中的item元素表示子菜单

  • 在代码中指定该菜单资源文件
MenuInflater inflater = new MenuInflater(this);inflater.inflate(R.menu.menu_main,menu);
  • 在代码中为其定义响应事件即可实现菜单功能
public boolean onOptionsItemSelected(MenuItem item) {        switch (item.getItemId()) {            case FONT_10:                editText.setTextSize(10);                break;            case FONT_12:                editText.setTextSize(12);                break;            case FONT_14:                editText.setTextSize(14);                break;            case FONT_16:                editText.setTextSize(16);                break;            case FONT_18:                editText.setTextSize(18);                break;            case FONT_RED:                editText.setTextColor(Color.RED);                break;            case FONT_GREEN:                editText.setTextColor(Color.GREEN);                break;            case FONT_BLUE:                editText.setTextColor(Color.BLUE);                break;            case PLAIN_ITEM:                break;        }        return true;    }

2.使用XML文件设置上下文菜单

  • XML文件
<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android">    <!-- 定义一组单选菜单项目 -->    <group android:checkableBehavior="single">        <!-- 定义三个菜单项 -->        <item            android:id="@+id/red"            android:title="@string/red_title"            android:alphabeticShortcut="r"/>        <item            android:id="@+id/green"            android:title="@string/green_title"            android:alphabeticShortcut="g"/>        <item            android:id="@+id/blue"            android:title="@string/blue_title"            android:alphabeticShortcut="b"/>    </group></menu>
  • 为控件注册上下文菜单
textView = (TextView) findViewById(R.id.textView);        //为该控件注册菜单项        registerForContextMenu(textView);
  • 为该菜单设置响应事件
public boolean onContextItemSelected(MenuItem item) {        switch (item.getItemId()) {            case R.id.red:                item.setChecked(true);                textView.setBackgroundColor(Color.RED);                break;            case R.id.blue:                    item.setChecked(true);                    textView.setBackgroundColor(Color.GREEN);                    break;            case R.id.green:                    item.setChecked(true);                    textView.setBackgroundColor(Color.BLUE);                    break;        }        return true;    }

三、为控件设置PopupMenu

PopupMenu可以作为一个控件的弹出式菜单显示在控件旁边

实现思路:
1.调用new PopupMenu(Context context,View view)创建菜单
2.调用MenuInflater的inflate()方法将菜单资源填充到PopupMenu中
3.调用PopupMenu的show()方法显示弹出式菜单

  • 菜单XML文件
<menu xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    tools:context="top.hellowoodes.menu.MainActivity">    <item android:id="@+id/search"        android:title="查找"/>    <item android:id="@+id/add"        android:title="添加"/>    <item android:id="@+id/edit"        android:title="编辑"/>    <item android:id="@+id/exit"        android:title="隐藏"/></menu>
  • 为控件设置点击按钮以显示弹出菜单
public void menu(View view) {        popupMenu = new PopupMenu(this,view);        getMenuInflater().inflate(R.menu.menu,popupMenu.getMenu());        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {            @Override            public boolean onMenuItemClick(MenuItem item) {                switch (item.getItemId()) {                    case R.id.exit:                       popupMenu.dismiss();                        break;                    default:                        Toast.makeText(MainActivity.this, item.getTitle(), Toast.LENGTH_SHORT).show();                }                return true;            }        });        popupMenu.show();    }
0 0
原创粉丝点击