A010-menu 资源,看完老板哭着让我留下来
// 添加子菜单
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:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<group android:id="@+id/group1">
<item
android:id="@+id/mi1"
android:title="item1" />
<item
android:id="@+id/mi2"
android:title="item2" />
</group>
<group android:id="@+id/group2">
<item
android:id="@+id/mi3"
android:title="item3" />
<item
android:id="@+id/mi4"
android:title="item4" />
</group>
</menu>
然后再 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
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
评论