PopupMenu项目图标 [英] PopupMenu Item Icons

查看:194
本文介绍了PopupMenu项目图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用有问题。我需要显示 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?

我的代码到目前为止;

My code so far below;

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"/>

这就是我看到的(红色方框)补充说o显示图像的去向;)

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 。有一个GitHub存储库,用于带有图标的Android兼容性弹出菜单,这可能会有所帮助。

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.

我希望这会有所帮助。

这篇关于PopupMenu项目图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆