弹出菜单项图标 [英] PopupMenu Item Icons
问题描述
我的应用程序有问题.我需要显示一个 PopupMenu
并且我需要这个菜单中的每个项目都包含文本和图像.图像应显示在文本的左侧.但它没有显示,有什么建议可以让它发生吗?
I have a problem with my app. I need to show a PopupMenu
and I need every item in this menu to contain text and an image. The images should be displayed to the left of the text. But it is not showing, any suggestions how to make it happen?
到目前为止我的代码;
public void showMenu(Button button) {
PopupMenu popupMenu = new PopupMenu(this, button);
popupMenu.getMenuInflater().inflate(R.menu.config_menu, popupMenu.getMenu());
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
switch (menuItem.getItemId()) {
还有我的选项菜单:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/dial"
android:icon="@drawable/phone"
android:title="@string/dialText"/>
<item
android:id="@+id/GPS"
android:icon="@drawable/gps"
android:title="@string/gpsText"/>
<item
android:id="@+id/Record"
android:icon="@drawable/record"
android:title="@string/recordText"/>
<item
android:id="@+id/notActive"
android:title="@string/not_active"/>
这就是我所看到的(添加了红色框以显示图像的位置);
This is what I see (red boxes added to show where the images should go);
推荐答案
遗憾的是 PopupMenu
默认不支持图标.
Unfortunately PopupMenu
does not support icons by default.
替代方法包括创建您自己的扩展PopupMenu
的类或更简单的切换到使用 ActionBar
.带有图标的Android兼容性弹出菜单有一个GitHub存储库,这可能会有所帮助.
Alternatives include creating your own class which extends PopupMenu
or more simply switching to using an ActionBar
. There is a GitHub repository for an Android Compatibility popup menu with icons, which might be helpful.
在这三个选项中,我建议使用 ActionBar
,因为 android 似乎大力推动使用 ActionBar
作为最佳实践,而不是其传统标题菜单指南.
Of the three options I would suggest going with the ActionBar
because android seem to be heavily pushing the use of the ActionBar
as best practice over the traditional header menus in their guides.
我希望这会有所帮助.
这篇关于弹出菜单项图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!