欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Android控件之菜单详解

程序员文章站 2022-06-30 18:19:46
...

选项菜单:

Android控件之菜单详解

上下文菜单:

Android控件之菜单详解

弹出菜单:

Android控件之菜单详解

第一种创建方式

//下面是选项菜单,上下文菜单,弹出菜单
创建代码及其



package com.example.androidthismenus;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

    private TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv=(TextView) findViewById(R.id.tv1);
        registerForContextMenu(tv);
    }
    //创建弹出菜单  的方法
    //按钮的点击事件
    public void button1(View v){
        //创建弹出菜单     参数1(上下文)(要显示的弹出组件,我传了按钮点击事件的V);
        PopupMenu popupMenu=new PopupMenu(this, v);
        //通过MenuInflater进行填充数据
        MenuInflater mInflater =popupMenu.getMenuInflater();
        //把定义好的menuXML资源文件填充到popupMenu当中
        mInflater.inflate(R.menu.main,popupMenu.getMenu() ); 
        popupMenu.show();
        Toast.makeText(getApplicationContext(), "hello", 1000).show();
    }
    //用于创建选项菜单的事件方法,在打开界面是会被自动调用
    @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);
        //参数1(组id)参数2(当前选项的id的id)参数3(项目排序的编号)参数4(项目的标题)
        menu.add(0, 100, 1, "设置游戏");
        menu.add(0, 200, 2, "开始游戏");
        menu.add(0, 300, 3, "退出游戏");
        menu.add(0, 400, 4, "开始游戏");
        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.
        int id = item.getItemId();
        switch (item.getItemId()) {
        case 100:
                Toast.makeText(getApplicationContext(), item.getTitle(), 1000).show();
            break;
        case 200:
            Toast.makeText(getApplicationContext(), item.getTitle(), 1000).show();
            break;
        case 300:
            Toast.makeText(getApplicationContext(), item.getTitle(), 1000).show();
            break;
        case 400:
            Toast.makeText(getApplicationContext(), item.getTitle(), 1000).show();
            break;
        }
        return super.onOptionsItemSelected(item);


    }
    //创建上下文菜单的代码:
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
        // TODO Auto-generated method stub
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.add(1, 100, 1, "红色");
        menu.add(1, 200, 2, "绿色");
        menu.add(1, 300, 3, "蓝色");
    }
    //上下文菜单被单击事件的代码:
    @Override
    public boolean onContextItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        switch (item.getItemId()) {
        case 100:
                Toast.makeText(getApplicationContext(),item.getTitle() ,1000).show();
                tv.setBackgroundColor(Color.RED);
                break;
        case 200:
                tv.setBackgroundColor(Color.GREEN);
                Toast.makeText(getApplicationContext(),item.getTitle() ,1000).show();
                break;
        case 300:
                tv.setBackgroundColor(Color.BLUE);
                Toast.makeText(getApplicationContext(),item.getTitle() ,1000).show();
            break;
        }
        return super.onContextItemSelected(item);

    }
}


第二种创建方式:

// 通过配置文件来在Activyt当中填充menu菜单

MenuInflater mInflater = getMenuInflater(); mInflater.inflate(R.menu.menu, menu); 
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/menu_new"
        android:icon="@android:drawable/ic_menu_help"
        android:title="新建"/>
    <item
        android:id="@+id/menu_delete"
        android:icon="@android:drawable/ic_menu_delete"
        android:title="删除"/>
    <item android:title="子菜单">
        <menu >
            <item android:id="@+id/menu_sub1"
                android:title="子菜单1"/>
            <item android:id="@+id/menu_sub2"
                android:title="子菜单2"/>
        </menu>
    </item>

</menu>


相关标签: 菜单