如何为弹出菜单项插入图标

时间:2014-03-10 11:59:36

标签: java android xml

我使用弹出式菜单inflater查看菜单项,但我没有在菜单列表中找到图标..我的代码请帮助我..

<item android:id="@+id/menu_messages" android:title="Messages"  android:icon="@drawable/mail"    />
<item android:id="@+id/menu_settings" android:title="Settings" android:icon="@drawable/setting" />
<item android:id="@+id/menu_logout" android:title="Logout" android:icon="@drawable/lock1" />

最终ImageView iv =(ImageView)findViewById(R.id.imageView2);      / **       *步骤1:创建弹出菜单的新实例       * /      final PopupMenu popupMenu = new PopupMenu(this,iv);

 /**
  * Step 2: Inflate the menu resource. Here the menu resource is
  * defined in the res/menu project folder
  */
// popupMenu.inflate(R.layout.listview_menu);
 popupMenu.inflate(R.menu.example);
 /**
  * Step 3: Call show() method on the popup menu to display the
  * menu when the button is clicked.
  */
 iv.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {

         popupMenu.show();
     }
 });


 /**
  *  Handle menu item clicks
  */
 popupMenu.setOnMenuItemClickListener(
         new PopupMenu.OnMenuItemClickListener() {
     @Override
     public boolean onMenuItemClick(MenuItem item) {
         switch (item.getItemId()) {
             case R.id.menu_messages:
                 startActivity(new Intent(WelcomeScreen.this,Contact.class));
                 break;
             case R.id.menu_settings:
                 startActivity(new Intent(WelcomeScreen.this,Settings.class));
                 break;
             case R.id.menu_logout:
                 startActivity(new Intent(WelcomeScreen.this,Login.class));
                 break;
         }
         return true;
     }
 });

1 个答案:

答案 0 :(得分:1)

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