以编程方式设置 PopupMenu 菜单项
Posted
技术标签:
【中文标题】以编程方式设置 PopupMenu 菜单项【英文标题】:Setting PopupMenu menu items programmatically 【发布时间】:2012-11-26 20:54:17 【问题描述】:我有一个PopupMenu
,我知道将菜单与其关联的常用方法是使用popup.getMenuInflater().inflate(R.menu.my_menu, popup.getMenu());
或类似的东西。我的问题是,我在菜单中有一组我想要的项目,我需要能够在 Java 中以编程方式更改它们。我怎样才能做到这一点?
【问题讨论】:
【参考方案1】:刚刚想通了;对于遇到同样问题的任何人,您只需这样做:
popup.getMenu().add(groupId, itemId, order, title);
对于您要添加的每个MenuItem
。
【讨论】:
【参考方案2】:只需创建弹出菜单,注册弹出窗口将在下面显示的视图,然后使用 getMenu() 方法添加值。不要忘记调用 show();
PopupMenu menu = new PopupMenu(this, view);
menu.getMenu().add("titleRes");
menu.show();
【讨论】:
【参考方案3】:试试这个:
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();
);
【讨论】:
【参考方案4】:在 popupmenu.xml 中定义 ids
<?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;
【讨论】:
这是一个更好的解释 你好。如何为图标执行此操作?【参考方案5】:这是一个设置和处理 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 等...
【讨论】:
【参考方案6】:@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();
【讨论】:
【参考方案7】:在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();
在 Kotlin 中
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()
【讨论】:
这是 android 中的动态弹出菜单。如果你想添加动态数据。例如: - 如果使用从 APIs 获取数据并在弹出窗口中显示数据,那么此代码非常有用。【参考方案8】:如果您正在寻找带有回调的生成弹出项目,请使用此。
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);
【讨论】:
【参考方案9】:来自字符串数组资源
值 > strings.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();
【讨论】:
以上是关于以编程方式设置 PopupMenu 菜单项的主要内容,如果未能解决你的问题,请参考以下文章