发布的apk版本中没有显示POPUPMENU图标

时间:2015-06-03 10:31:46

标签: android drop-down-menu contextmenu menuitem

我遵循本教程,当我处于调试模式时它完全正常但是当我在发布模式下生成apk时,图标不会起作用我的意思是那些赢了出现原因??

以下是我关注的教程的链接 Tutorial here

custom_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
 <item android:id="@+id/....."
    android:title="------"
    android:icon="....."
    app:showAsAction="always"/>
 <item android:id="@+id/....."
    android:title="------"
    android:icon="....."
    app:showAsAction="always"/>
 <item android:id="@+id/....."
    android:title="------"
    android:icon="....."
    app:showAsAction="always"/>
<item android:id="@+id/....."
    android:title="------"
    android:icon="....."
    app:showAsAction="always"/>
</menu>

这是java代码

  View menuItemView = getActivity().findViewById(R.id.overflow);
            PopupMenu popupMenu = new PopupMenu(getActivity(), menuItemView);
            popupMenu.inflate(R.menu.custom_menu);
            //
            Object menuHelper;
            Class[] argTypes;
            Field fMenuHelper = null;
            try {
                fMenuHelper = PopupMenu.class.getDeclaredField("mPopup");
                fMenuHelper.setAccessible(true);
                menuHelper = fMenuHelper.get(popupMenu);
                argTypes = new Class[]{boolean.class};
                menuHelper.getClass().getDeclaredMethod("setForceShowIcon", argTypes).invoke(menuHelper, true);
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }

1 个答案:

答案 0 :(得分:0)

这是因为proguard模糊了PopupMenu类名。要使图标在发行版apk中有效,请在proguard.cfg文件中包含以下代码:

-keepclassmembernames class android.support.v7.widget.PopupMenu {
private android.support.v7.internal.view.menu.MenuPopupHelper mPopup; }

-keepclassmembernames class android.support.v7.internal.view.menu.MenuPopupHelper {
public void setForceShowIcon(boolean); }