PopupMenu Item Icons

柔情痞子 提交于 2019-12-19 10:13:31

问题


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()) {

And my options menu:

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


回答1:


Unfortunately PopupMenu does not support icons by default.

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.

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.

I hope this helps.



来源:https://stackoverflow.com/questions/27919595/popupmenu-item-icons

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!