先展示效果:
pic 1.png
pic 2.png
再亮代码:
/**
* view 表示要附着的控件, menuList 表示要展示的菜单列表,
* DetailActivity 表示当前页面的 Activity
*/
void showPopMenu(View view) {
PopupMenu popupMenu = new PopupMenu(DetailActivity.this, view);
android.view.Menu menu_more = popupMenu.getMenu();
int size = menuList.size();
for (int i = 0; i < size; i++) {
menu_more.add(android.view.Menu.NONE, android.view.Menu.FIRST + i, i, menuList.get(i).getTitle());
}
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
int i = item.getItemId();
startMenu(menuList.get(i-1));
return true;
}
});
popupMenu.show();
}
参考链接
Android 弹出菜单
网友评论