1.右击res-->New-->Directory输入文件名menu,在此文件夹下新建main菜单文件:右击menu-->New-->Menu resource file

2.在main.xml中添加如下代码:

<menu xmlns:andro/>
</menu>

3.活动中重写OnCreateOptionsMenu()方法

1 public boolean onCreateOptionsMenu(Menu menu){
2     getMemuInflater().inflate(R.menu.main,menu);  //获取MenuInflater对象,并调用inflate方法创建菜单。
3     return true;    
4 }

4.响应item点击事件

public boolean onOptionsItemSelected(MenuItem item){
    switch(item.getItemid()){
        case R.id.add_item:
        Toast.makeText(this,"点击了Add",Toast.LENGTH_SHORT).show();
break;
     case R.id.remove_item:
      
Toast.makeText(this,"点击了Add",Toast.LENGTH_SHORT).show();
      break;
     default:
    }
  return true; }

 

相关文章:

  • 2022-12-23
  • 2021-11-23
  • 2021-08-06
  • 2022-12-23
  • 2021-07-19
  • 2021-09-28
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-12-30
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案