以编程方式设置PopupMenu菜单项

时间:2012-12-09 02:28:29

标签: android android-menu

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

8 个答案:

答案 0 :(得分:118)

刚想通了;对于遇到同样问题的人,你只需要这样做:

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

您要添加的每个MenuItem

答案 1 :(得分:32)

只需创建弹出菜单,注册弹出窗口将在下面显示的视图,并使用getMenu()方法添加值。别忘了打电话给show();

PopupMenu menu = new PopupMenu(this, view);

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

答案 2 :(得分:20)

试试这个:

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();
    }
});

答案 3 :(得分:13)

在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;
        }
    }

答案 4 :(得分:10)

以下是设置和处理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等...

答案 5 :(得分: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.menu1, Menu.NONE,"slot2");
        popupMenu.getMenu().add(R.id.menuGroup, R.id.menu1, 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();
    }

答案 6 :(得分:0)

 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();
    }

答案 7 :(得分:0)

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

Java

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);