以编程方式设置PopupMenu菜单项


70

我有一个,PopupMenu而且我知道将菜单关联到该菜单的通常方法是使用popup.getMenuInflater().inflate(R.menu.my_menu, popup.getMenu());或类似方法。我的问题是,菜单中有很多项目,我需要能够以Java编程方式更改它们。我怎样才能做到这一点?

Answers:


131

只是想通了;对于遇到相同问题的任何人,您只需:

popup.getMenu().add(groupId, itemId, order, title);

对于MenuItem您要添加的每个对象。


38

只需创建弹出菜单,注册弹出窗口将在其下方显示的视图,并使用getMenu()方法添加值。不要忘记调用show();。

PopupMenu menu = new PopupMenu(this, view);

menu.getMenu().add("titleRes");
menu.show();

26

尝试这个:

Dynamic_PopUpMenu.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        PopupMenu menu = new PopupMenu(DialogCheckBox.this, v);
        menu.getMenu().add("AGIL");         
        menu.getMenu().add("AGILarasan");
        menu.getMenu().add("Arasan");
        menu.show();
    }
});

17

在popupmenu.xml中定义ID

<?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">

        <item
            android:id="@+id/slot1"
            app:showAsAction="ifRoom|withText"
            android:title="Movies"
            android:visible="true"/>
        <item
            android:id="@+id/slot2"
            app:showAsAction="ifRoom|withText"
            android:title="Music"
            android:visible="true"/>
        <item
            android:id="@+id/slot3"
            app:showAsAction="ifRoom|withText"
            android:title="Comedy"
            android:visible="true"/>

</menu>  

PopupMenu popupMenu = new PopupMenu(FullMenuActivity.this, view);                      
popupMenu.setOnMenuItemClickListener(FullMenuActivity.this);
popupMenu.getMenu().add(1, R.id.slot1, 1, "slot1");
popupMenu.getMenu().add(1,R.id.slot2,2,"slot2");
popupMenu.getMenu().add(1,R.id.slot3,3,"slot3");
popupMenu.show();

 @Override
    public boolean onMenuItemClick(MenuItem item) {

        switch (item.getItemId()) {
            case R.id.slot1:
                SessionManager.selected_slot = item.getTitle().toString();
                Toast.makeText(this, "slot1 Clicked", Toast.LENGTH_SHORT).show();
                return true;
            case R.id.slot2:
                SessionManager.selected_slot = item.getTitle().toString();
                Toast.makeText(this, "slot2 Clicked", Toast.LENGTH_SHORT).show();
                return true;
            case R.id.slot3:
                SessionManager.selected_slot = item.getTitle().toString();
                Toast.makeText(this, "slot3 Clicked", Toast.LENGTH_SHORT).show();
                return true;
            default:
                return true;
        }
    }

14

这是设置和处理ID的完整解决方案:

        this.findViewById(R.id.hamburger_menu).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            PopupMenu menu = new PopupMenu(getApplicationContext(), v);
            menu.getMenu().add(Menu.NONE, 1, 1, "Share");
            menu.getMenu().add(Menu.NONE, 2, 2, "Comment");
            menu.show();

            menu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {

                    int i = item.getItemId();
                    if (i == 1) {
                        //handle share
                        return true;
                    } else if (i == 2) {
                        //handle comment
                        return true;
                    } else {
                        return false;
                    }
                } 

            });
         }
     });

注意:例如,共享和注释,也可以为数字1,2放置常量,以使代码更具可读性。

另外,我放,Menu.NONE是因为我不在乎对菜单项进行分组。如果要使组集的组ID = 1、2等...


3

@Voora Tarun给出了一个很好的答案,我以此为基础:首先,代替使用假mune.xml资源,我认为更好的方法是创建ids文件:

ids.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item name="menuGroup" type="id"/>
    <item name="menu1" type="id"/>
    <item name="menu2" type="id"/>
    <item name="menu3" type="id"/>
</resources>

然后,您可以执行以下操作:

private void showPopup(final View anchor) {
        PopupMenu popupMenu = new PopupMenu(anchor.getContext(), anchor);
        popupMenu.getMenu().add(R.id.menuGroup, R.id.menu1, Menu.NONE, "slot1");
        popupMenu.getMenu().add(R.id.menuGroup, R.id.menu2, Menu.NONE,"slot2");
        popupMenu.getMenu().add(R.id.menuGroup, R.id.menu3, Menu.NONE,"slot3");
        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                Toast.makeText(anchor.getContext(), item.getTitle() + "clicked", Toast.LENGTH_SHORT).show();
                return true;
            }
        });
        popupMenu.show();
    }

2
 private void getPopup(final TextView textView, ArrayList<String> arrayList) {
        final PopupMenu popupMenu = new PopupMenu(sContext, textView);
        for (int i = 0; i < arrayList.size(); i++) {
            popupMenu.getMenu().add(i, Menu.FIRST, i, arrayList.get(i));
        }
        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                textView.setText(item.getTitle());
                return false;
            }
        });
        popupMenu.show();
    }

这是android中的动态弹出菜单。如果您想添加动态数据。例如:-如果使用从Apis获取数据并在弹出窗口中显示数据,则此代码非常有用。
萨法尔·巴蒂亚

0

如果要查找带有回调的生成的弹出项,请使用此选项。

爪哇

public static void popupMenu(final Context context, View anchor, final LinkedHashMap<String,IPopupMenu> options){
        PopupMenu popupMenu = new PopupMenu(context, anchor);
        for(String key : options.keySet()){
            popupMenu.getMenu().add(key);
        }
        popupMenu.getMenuInflater().inflate(R.menu.popup_menu,popupMenu.getMenu());
        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem menuItem) {

                options.get(menuItem.getTitle()).onClick();
                return true;
            }
        });

        popupMenu.show();
    }

public interface IPopupMenu{
        void onClick();
    }

XML格式

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

</menu>

LinkedHashMap<String,IPopupMenu> menu_items = new LinkedHashMap<>();
menu_items.put("Item Name", new Utils.IPopupMenu() {
         @Override
         public void onClick() {
            //do your code
         }
});
menu_items.put("My Item", new Utils.IPopupMenu() {
         @Override
         public void onClick() {
            //do code
         }
});

//you can anchor the popup menu to whatever you like. i.e Button, TextView, View.
popupMenu(context,button,menu_items);
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.