// 添加子菜单
SubMenu subMenu = menu.addSubMenu(1, ITEM5, 5, “子菜单1”);
subMenu.add(1, ITEM7, 1, “子菜单项1”);
subMenu.add(1, ITEM8, 2, “子菜单项2”);
subMenu.add(1, ITEM9, 3, “子菜单项3”);
menu.addSubMenu(1, ITEM6, 6, “子菜单2”);
// Inflate the menu; this adds items to the action bar if it is present.
// getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
如何响应菜单点击事件?
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case ITEM1:
Toast.makeText(this,“你点击了” + item.getTitle(), Toast.LENGTH_SHORT).show();
break;
case ITEM2:
Toast.makeText(this,“你点击了” + item.getTitle(), Toast.LENGTH_SHORT).show();
break;
case ITEM3:
Toast.makeText(this,“你点击了” + item.getTitle(), Toast.LENGTH_SHORT).show();
break;
case ITEM4:
Toast.makeText(this,“你点击了” + item.getTitle(), Toast.LENGTH_SHORT).show();
break;
case ITEM5:
Toast.makeText(this,“你点击了” + item.getTitle(), Toast.LENGTH_SHORT).show();
break;
case ITEM6:
Toast.makeText(this,“你点击了” + item.getTitle(), Toast.LENGTH_SHORT).show();
break;
case ITEM7:
Toast.makeText(this,“你点击了” + item.getTitle(), Toast.LENGTH_SHORT).show();
break;
case ITEM8:
Toast.makeText(this,“你点击了” + item.getTitle(), Toast.LENGTH_SHORT).show();
break;
case ITEM9:
Toast.makeText(this,“你点击了” + item.getTitle(), Toast.LENGTH_SHORT).show();
break;
}
return true;
}
效果如下:
xml文件
这种方式可以让我们开发者更方便得指定菜单,这样我们就不用去设定指定的itemId,而是由Android为我们随意生成指定id,这样的话我们在响应的时候,根据在xml指定id来判断即可。
eg:
xmlns:app=“http://schemas.android.com/apk/res-auto”
xmlns:tools=“http://schemas.android.com/tools”
tools:context=".MainActivity">
<item
android:id="@+id/mi1"
android:title=“item1” />
<item
android:id="@+id/mi2"
android:title=“item2” />
<item
android:id="@+id/mi3"
android:title=“item3” />
<item
android:id="@+id/mi4"
android:title=“item4” />
然后再onCreateOptionMenu中这样加载:
getMenuInflater().inflate(R.menu.menu_main, menu);
响应事件就不说了,跟硬编码的响应方式一样。
Context Menu(上下文菜单)
上下文菜单跟选项菜单有点区别,后者是响应Activity的操作,而前者是响应View的操作。
如何使用?
注册上下文菜单
private Button contextMenuButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
contextMenuButton = (Button) findViewById(R.id.button);
registerForContextMenu(contextMenuButton);
}
生成上下文菜单
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
// set context menu title
menu.setHeaderTitle(“文件操作”);
// add context menu item
menu.add(0, 1, Menu.NONE, “发送”);
menu.add(0, 2, Menu.NONE, “标记为重要”);
menu.add(0, 3, Menu.NONE, “重命名”);
menu.add(0, 4, Menu.NONE, “删除”);
super.onCreateContextMenu(menu, v, menuInfo);
}
响应上下文
@Override
public boolean onContextItemSe
《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
浏览器打开:qq.cn.hn/FTe 免费领取
lected(MenuItem item) {
// 得到当前被选中的item信息
AdapterView.AdapterContextMenuInfo menuInfo = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
Log.v(TAG, “context item seleted ID=” + menuInfo.id);
switch (item.getItemId()) {
case 1:
// do something
break;
case 2:
// do something
break;
case 3:
// do something
break;
case 4:
// do something
break;
default:
return super.onContextItemSelected(item);
}
return true;
}
ok,当我们长按指定上下文的view,就会弹出上下文菜单:
Popup Menu (弹出菜单)
这个菜单跟Context Menu类似,也是响应View的操作的,比如我们响应一个按钮,点击按钮就弹出菜单项,它的操作上就不用像上下文菜单那样要长按。
eg:
public void showPopupMenu(View view) {
PopupMenu popupMenu = new PopupMenu(this, view);
MenuInflater inflater = popupMenu.getMenuInflater();
inflater.inflate(R.menu.menu_main, popupMenu.getMenu());
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.mi1:
// do something
break;
case R.id.mi2:
// do something