代码与范例:
1 2 3 4 5 6 7 8 9 10 11 12 |
final View menuButton = findViewById(R.id.menuButton); PopupMenu popup = new PopupMenu(MainActivity.this, menuButton); popup.getMenuInflater().inflate(R.menu.menus, popup.getMenu()); popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { Toast.makeText(MainActivity.this, "menu clicked", Toast.LENGTH_SHORT).show(); return true; } }); popup.show(); |
说明:PopupMenu 是 Android API Level11 引入的新特性,可以在屏幕上渲染一个弹出式的菜单窗口,用于显示一些常用的操作。