如果我使用示例A ,溢出菜单的背景为白色,正如预期的那样。但如果我使用样本B ,则背景为黑色。我需要样品B因为我想要显示图标。但我需要保持背景白色。我该怎么做?
示例A
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_dogs, menu);
}
样本B
findViewById(R.id.moreMenuBtn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {
PopupMenu popupMenu = new PopupMenu(v.getContext(), v);
popupMenu.inflate(R.menu.menu_dogs);
forceShowIcon(popupMenu);
popupMenu.show();
}
public static void forceShowIcon(PopupMenu popup) {
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();
}
}
应用主题
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
答案 0 :(得分:1)
试试这个:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:popupMenuStyle">@style/PopupMenu</item>
</style>
<style name="PopupMenu" parent="AppTheme">
<item name="android:popupBackground">@color/colorPrimary</item>
<item name="android:textColor">@color/colorAccent</item>
</style>
答案 1 :(得分:0)
将popupMenu样式添加到 AppTheme :
<style name="AppTheme" parent="android:Theme.Light.NoActionBar">
<item name="android:popupMenuStyle">@style/PopupMenu</item>
</style>
<style name="PopupMenu" parent="@android:style/Widget.PopupMenu">
<item name="android:popupBackground">@android:color/white</item>
</style>
<强>的manifest.xml:强>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
.............
</application>