77

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

4

9 回答 9

143

刚刚想通了;对于遇到同样问题的任何人,您只需执行以下操作:

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

对于MenuItem您要添加的每个。

于 2012-12-09T02:53:37.600 回答
47

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

PopupMenu menu = new PopupMenu(this, view);

menu.getMenu().add("titleRes");
menu.show();
于 2013-01-12T09:11:40.333 回答
27

尝试这个:

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();
    }
});
于 2016-12-07T04:39:46.493 回答
21

在 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;
        }
    }
于 2015-12-28T14:24:16.760 回答
16

这是一个设置和处理 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 等...

于 2016-09-18T15:40:24.113 回答
5

@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();
    }
于 2016-09-06T08:16:31.473 回答
3

在java中

     private void getPopup(Activity sContext,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();
        }

在科特林

private fun getPopup(
    sContext: Activity,
    textView: TextView,
    arrayList: ArrayList<String>
) {
    val popupMenu = PopupMenu(sContext, textView)
    for (i in 0 until arrayList.size) {
        popupMenu.getMenu().add(i, Menu.FIRST, i, arrayList[i])
    }
    popupMenu.setOnMenuItemClickListener(object : 
        PopupMenu.OnMenuItemClickListener {
        override fun onMenuItemClick(item: MenuItem): Boolean {
            textView.setText(item.getTitle())
            return false
        }
    })
    popupMenu.show()
}
于 2019-09-17T10:13:44.813 回答
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);
于 2020-04-21T04:09:25.230 回答
0

来自字符串数组资源

值 > 字符串.xml

<string-array name="MainCategories">
        <item>Crop</item>
        <item>Animals</item>
        <item>Equipment</item>
        <item>Other</item>
    </string-array>

Java代码:

    String []categories=getResources().getStringArray(R.array.MainCategories);
    PopupMenu popupMenu=new PopupMenu(MainActivity.this,sortExpenseBtn);

                for(String item: categories)
                    popupMenu.getMenu().add(item);

                popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        String selected = item.getTitle();
                        return true;
                    }
                });
                popupMenu.show();
于 2021-09-17T12:55:37.663 回答