【Android笔记】选项菜单

原创
2016/09/30 22:51
阅读数 98

使用选项菜单OptionMenu

1.创建选项菜单

重写onCreateOptionsMenu():

2.设置菜单项

  1. 通过xml设置:Menunflater.inflate();
  2. 代码动态设置:menu.add();

3.设置菜单项监听

重写onOptionsItemSelected();

实例一:通过xml设置菜单

1.在资源目录res下创建menu资源文件夹

2.在menu文件夹下创建menu.xml文件

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/action_item1"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="菜单一"/>

    <item
        android:id="@+id/action_item2"
        android:orderInCategory="101"
        android:showAsAction="never"
        android:title="菜单二"/>

    <item
        android:id="@+id/action_item3"
        android:orderInCategory="102"
        android:showAsAction="never"
        android:title="菜单三"/>

</menu>

3.重写onCreateOptionsMenu()与onOptionsItemSelected()方法

package com.example.demo15;

import android.content.Intent;
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);
    }

    /**
     * 添加菜单
     * @param menu
     * @return
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.menu, menu);

        return true;
    }

    /**
     * 菜单事件监听
     * @param item
     * @return
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.action_item1:
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                item.setIntent(intent);
                break;
            case R.id.action_item2:
                Toast.makeText(MainActivity.this, "菜单二", Toast.LENGTH_SHORT).show();
                break;
            case R.id.action_item3:
                Toast.makeText(MainActivity.this, "菜单三", Toast.LENGTH_SHORT).show();
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}

效果:

实例二:动态设置

package com.example.demo15;

import android.content.Intent;
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);
    }

    /**
     * 添加菜单
     * @param menu
     * @return
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        //getMenuInflater().inflate(R.menu.menu, menu);

        menu.add(1, 100, 1, "菜单一");
        menu.add(1, 101, 1, "菜单二");
        menu.add(1, 102, 1, "菜单三");
        MenuItem menu1 = menu.add(1, 103, 1, "菜单四");
        menu1.setTitle("新菜单");

        return true;
    }

    /**
     * 菜单事件监听
     * @param item
     * @return
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case 100:
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                item.setIntent(intent);
                break;
            case 101:
                Toast.makeText(MainActivity.this, "菜单二", Toast.LENGTH_SHORT).show();
                break;
            case 102:
                Toast.makeText(MainActivity.this, "菜单三", Toast.LENGTH_SHORT).show();
                break;
            case 103:
                Toast.makeText(MainActivity.this, "菜单四", Toast.LENGTH_SHORT).show();
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}

效果:

 

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部