如何更改背景颜色选项菜单xamarin android

时间:2017-08-23 22:12:48

标签: android xamarin popupmenu

我尝试将背景颜色选项菜单从黑色更改为白色 但我的代码不起作用。我也看到了这个链接How to change background color popup menu android,但它对我不起作用。

样式              

  <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>

1 个答案:

答案 0 :(得分:0)

  

如何更改背景颜色选项菜单

您是否控制了传递给构造函数的上下文?创建PopupMenu课程时,请使用Activity上下文,如下所示:

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

您是使用android.widget.PopupMenu还是android.support.v7.widget.PopupMenu?它们受不同主题属性的支配:

 <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>

效果如this