美文网首页Android Dev
Android 动态添加弹出菜单

Android 动态添加弹出菜单

作者: plusend | 来源:发表于2016-03-09 16:09 被阅读1698次

    先展示效果:

    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 弹出菜单

    相关文章

      网友评论

        本文标题:Android 动态添加弹出菜单

        本文链接:https://www.haomeiwen.com/subject/mtxtlttx.html