0

i try change background color option menu from black to white but my code not work.i see this link too How to change background color popup menu android but its not work for me.

styles

  <style name="Theme.DesignDemo" parent="Base.Theme.DesignDemo">
  </style>

  <style name="Base.Theme.DesignDemo" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/ColorPrimary</item>
      <item name="popupMenuStyle">@style/PopupMenu</item>
    <item name="colorPrimaryDark">@color/ColorPrimaryDark</item>
     </style>
<style name="PopupMenu" parent="Widget.AppCompat.PopupMenu">
    <item name="android:popupBackground">@android:color/white</item>

</style>

</resources>

popup_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">
  <!--///showAsAction="always" ///-->
  <item android:id="@+id/action_settings" android:title="Share" showAsAction="always" />
  <item android:id="@+id/action_settings1" android:title="Bluetooth" showAsAction="always" />
  <item android:id="@+id/action_settings2" android:title="Exit" showAsAction="always" />
  <!--/android:showAsAction="ifRoom"/-->
  <item android:id="@+id/action_settings3" android:title="Share" android:showAsAction="ifRoom" />
  <item android:id="@+id/action_settings4" android:title="Bluetooth" android:showAsAction="ifRoom" />
</menu>
controleng
  • 5
  • 1
  • 7

1 Answers1

0

How to change background color option menu

Have you controlled the context you passed to the constructor? When you create a PopupMenu class, please use the Activity context like this :

PopupMenu popup = new PopupMenu(this, anchorView);//Not BaseContext

Whether you use android.widget.PopupMenu or android.support.v7.widget.PopupMenu? They governed by different theme attributes :

 <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
     <!-- if using android.widget.PopupMenu -->
     <item name="android:popupMenuStyle">@style/PopupMenu</item>
     <!-- if using android.support.v7.widget.PopupMenu -->
     <item name="popupMenuStyle">@style/PopupMenu</item>
 </style/>

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

Effect like this.

York Shen
  • 9,014
  • 1
  • 16
  • 40