سؤال

In my app I create a PopupMenu like this:

public void openPopup(View v) {
    PopupMenu menu = new PopupMenu(MainActivity.this, v);
    menu.inflate(R.menu.menu_popup);
    menu.show();
}

I've styled my popups like this:

<style name="Theme.MyTheme" parent="@style/Theme.AppCompat.Light">
    <item name="popupMenuStyle">@style/PopupMenu.MyTheme</item>
    <item name="android:popupMenuStyle">@style/PopupMenu.MyTheme</item>    
</style>

<style name="PopupMenu.MyTheme" parent="@style/Widget.AppCompat.Light.PopupMenu">
    <item name="android:popupBackground">@color/black</item>
    <item name="android:textColor">@color/white</item>
</style>

I also apply the theme which works fine otherwise

<application
    android:theme="@style/Theme.MyTheme"
    ... >

Yet the popup shows with a white background and black text. What can be wrong?

هل كانت مفيدة؟

المحلول

It took some digging, but I figured this out eventually.Either only popupMenuStyle or popupWindowStyle needs to be overwritten. I can't remember which one at the moment.

I'm not sure that this works with the AppCompat library, especially using listChoiceBackgroundIndicator. I haven't tested it on anything that targets something less than 14.

<style name="Theme.MyAppTheme" parent="android:Theme.Holo.Light">
<item name="android:popupMenuStyle">@style/MyAwesomeBackground.PopupStyle</item>
<item name="android:popupWindowStyle">@style/MyAwesomeBackground.PopupStyle</item>
<item name="android:textAppearanceLargePopupMenu">@style/CustomTextStyle</item>
<item name="android:textAppearanceSmallPopupMenu">@style/CustomTextStyle</item>
<item name="android:listChoiceBackgroundIndicator">@drawable/custom_list_selector</item>
</style>
<style name="AwesomeBackground.PopupStyle" parent="Widget.PopupMenu">
   <item name="android:popupBackground">@drawable/custom_background</item>
</style>
<style name="CustomTextStyle">
<!-- some custom text stylings -->
</style>

نصائح أخرى

On your menu, set your items like: onClick="showPopUp", ensure such method exists.

public void showPopUp(View v)
{
  Context context = new ContextThemeWrapper(this,R.style.MyMaterialTheme);

  PopupMenu popupMenu=new PopupMenu(context,v);

  MenuInflater inflater=popupMenu.getMenuInflater();
  inflater.inflate(R.menu.button_menu,popupMenu.getMenu());
}

In your styles.xml, add this:

   <style name="MyMaterialTheme"  parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="windowActionBar">false</item>
    <item name="android:textColorPrimary">@color/black</item>
    <item name="android:textStyle">bold</item>
    <item name="android:iconifiedByDefault">true</item>
    <item name="android:backgroundTint">@color/orange</item></style>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top