动态添加菜单栏Pluse

package com.example.optionsmenu;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;


public class MainActivity extends Activity {

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


    @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);
    	MenuItem menuItem=menu.add(1,100,1,"菜单一");
    	menuItem.setTitle("aaa");
    	menuItem.setIcon(R.drawable.ic_launcher);//api>=11的时候不显示图标
    	menu.add(1,101,1,"菜单二");
    	menu.add(1,102,1,"菜单三");
    	menu.add(1,103,1,"菜单四");
    	menu.add(1,104,1,"菜单五");
    	menu.add(1,105,1,"菜单六");
    	menu.add(1,106,1,"菜单七");
        return true;
    }
    
    //重写onOptionsItemSelected方法
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	// TODO Auto-generated method stub
    	switch (item.getItemId()) {
		case 100:
			//Toast.makeText(MainActivity.this,"点击了菜单一",Toast.LENGTH_LONG).show();
			Intent intent=new Intent(MainActivity.this,SecondActivity.class);
			item.setIntent(intent);
			break;
        case 101:
        	Toast.makeText(MainActivity.this,"点击了菜单二",Toast.LENGTH_LONG).show();
			break;
        case 102:
        	Toast.makeText(MainActivity.this,"点击了菜单三",Toast.LENGTH_LONG).show();
			break;
        case 103:
        	Toast.makeText(MainActivity.this,"点击了菜单四",Toast.LENGTH_LONG).show();
			break;
        case 104:
        	Toast.makeText(MainActivity.this,"点击了菜单五",Toast.LENGTH_LONG).show();
			break;
        case 105:
        	Toast.makeText(MainActivity.this,"点击了菜单六",Toast.LENGTH_LONG).show();
			break;
        case 106:
        	Toast.makeText(MainActivity.this,"点击了菜单七",Toast.LENGTH_LONG).show();
			break;
		default:
			break;
		}
    	return super.onOptionsItemSelected(item);
    }
//    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();
//        if (id == R.id.item_menu1) {
//            return true;
//        }
//        return super.onOptionsItemSelected(item);
//    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值