我已经为我的一个Android应用程序实现了带有图标的自定义弹出菜单。在这里,我设置的图标也应用了setForceShowIcon
。这是以下代码
final PopupMenu popupMenu = new PopupMenu(mContext, shareFeed);
popupMenu.inflate(R.menu.custom_share_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();
}
final String des = map.get("title") + "\n" + map.get("description");
final String message = Html.fromHtml(des).toString() + "\n" + mContext.getResources().getString(R.string.app_url);
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.whatsapp:
if (PerformCommonOps.internetConnection(mContext))
PerformCommonOps.shareFeedsOnWhatsapp(mContext, message);
else
Snackbar.make(coordinatorLayout, "Please ensure you have a working internet connection", Snackbar.LENGTH_SHORT).show();
return true;
case R.id.facebook:
if (PerformCommonOps.internetConnection(mContext))
PerformCommonOps.shareToFacebbok(mContext, mContext.getResources().getString(R.string.app_url), view, (Activity) mContext);
else
Snackbar.make(coordinatorLayout, "Please ensure you have a working internet connection", Snackbar.LENGTH_SHORT).show();
return true;
case R.id.text:
if (PerformCommonOps.internetConnection(mContext))
PerformCommonOps.sendText(mContext, message);
else
Snackbar.make(coordinatorLayout, "Please ensure you have a working internet connection", Snackbar.LENGTH_SHORT).show();
return true;
case R.id.googleplus:
if (PerformCommonOps.internetConnection(mContext))
PerformCommonOps.shareOnGooglePlus(mContext, Html.fromHtml(des).toString(), mContext.getResources().getString(R.string.app_url), view);
else
Snackbar.make(coordinatorLayout, "Please ensure you have a working internet connection", Snackbar.LENGTH_SHORT).show();
return true;
default:
common();
return true;
}
}
});
popupMenu.show();
当我在调试模式中运行应用程序时显示图标,但在发布模式中使用 Proguard 时图标显示没有显示。
这是我setForceShowIcon
######################################
####### POPUpMenu configurations######
######################################
-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); }
请让我知道哪里&我做错了什么。的 TIA
答案 0 :(得分:1)
proguard-rules.pro
中的这一行解决了我的问题:
-keep class android.support.v7.widget.** { *; }
答案 1 :(得分:0)
由于您在自定义弹出类中使用反射,因此需要避免使用反射。
-keep public class * extends com.yoursite.android.yourappname.YourClassName
-keepclassmembers class * extends com.yoursite.android.yourappname.YourClassName{
public <init>(android.content.Context);
}
答案 2 :(得分:0)
由于setForceShowIcon
是android.support.v7.view.menu.MenuPopupHelper
我的成员,一般来说,将这些行添加到我的proguard规则中并且效果非常好:
-keep class * extends android.support.v7.** { *; }
-keepclassmembernames class * extends android.support.v7.** { *; }