图标不显示在菜单中

时间:2016-04-06 06:41:50

标签: android android-menu

我想在android中添加带有菜单标题的图标,但不会显示标题。下面是我的menu.xml代码

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">

<item android:id="@+id/rate_app"
    android:title="Rate App"
    android:orderInCategory="100"
    app:showAsAction="never"
    app:icon="@drawable/ic_star_half_24dp"/>

我还使用了android:icon = "@drawable/ic_star_half_24dp",但它没有显示菜单标题

3 个答案:

答案 0 :(得分:2)

如果您的预期行为是在Product.count

上显示图标

toolbar应更改为app:showAsAction="never"ifRoom

如果您想在always上显示icon,则需要自定义overflow menu并使用弹出窗口。

答案 1 :(得分:1)

试试这个,

<menu xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
 >

 <item
    android:id="@+id/A"
    android:icon="@drawable/ic_a"
    android:title="Name A"
    app:showAsAction="always"/>
 <item
    android:id="@+id/B"
    android:icon="@drawable/ic_b"
    android:title="Name B"
    app:showAsAction="always"/>

      <item
    android:id="@+id/a_More"
    android:icon="@drawable/ic_menu_overflow"
    app:showAsAction="always"
    android:title="More">
    <menu>

        <item 
            android:id="@+id/C"
            android:icon="@drawable/ic_c"
            android:title="Name C"
            app:showAsAction="never" />


        <item 
            android:id="@+id/D"
            android:title="Name D"
            app:showAsAction="never" />

        <item 
            android:id="@+id/E"
            android:icon="@drawable/icon_3"
            android:title="Name E"
            app:showAsAction="never" />    


       </menu>
     </item>
  </menu>

答案 2 :(得分:0)

If you are using popup menu just copy the below code and run it, you will get icons in popupmenu

PopupMenu popup = new PopupMenu(getApplicationContext(), view);

try {
    Field[] fields = popup.getClass().getDeclaredFields();
    for (Field field : fields) {
        if ("mPopup".equals(field.getName())) {
            field.setAccessible(true);
            Object menuPopupHelper = field.get(popup);
            Class<?> classPopupHelper = Class.forName(menuPopupHelper
                    .getClass().getName());
            Method setForceIcons = classPopupHelper.getMethod(
                    "setForceShowIcon", boolean.class);
            setForceIcons.invoke(menuPopupHelper, true);
            break;
        }
    }
} catch (Exception e) {
    e.printStackTrace();
}

    popup.getMenuInflater()
                    .inflate(R.menu.publisher, popup.getMenu());

                    //registering popup with OnMenuItemClickListener
                    popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                        public boolean onMenuItemClick(MenuItem item) {

                            switch (item.getItemId()) {
                            case R.id.menu:
                                //your function
                                return true;
                                                        default:
                                break;
                            }
                            return false;
                        }
                    });
                    popup.show();