popup menu background color change not working

岁酱吖の 提交于 2021-01-05 10:24:40

问题


i am begineer in android. I want to popup menu background color change.I try this code for background color change but its not working for me.i don't know why not working this code.

<!--popup menu item style-->
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.MaterialComponents.Light">
    <item name="android:textColor">@color/colorWhite</item>
    <item name="android:popupBackground">@color/colorBlack</item>
    <item name="actionOverflowMenuStyle">@style/CMOptionsMenu</item>
    <item name="android:divider">@color/colorWhite</item>
    <item name="android:dividerHeight">1dp</item>
</style>
<style name="CMOptionsMenu" parent="Widget.AppCompat.PopupMenu.Overflow">
    <item name="android:popupBackground">@color/colorBlack</item>
    <item name="overlapAnchor">false</item>
    <item name="android:divider">@color/colorRegisterHeader</item>
    <item name="android:dividerHeight">1dp</item>
</style>

//this activity code for popup menu

            PopupMenu popup = new PopupMenu(HomeActivity.this, v);
            popup.setOnMenuItemClickListener(HomeActivity.this);
            popup.inflate(R.menu.popup_menu);

回答1:


You can customize the background color using the android:popupBackground attribute.

  <!-- Popup Menu -->   
  <style name="MyPopup" parent="@style/Widget.MaterialComponents.PopupMenu">
    <item name="android:popupBackground">@color/custom</item>
  </style>

You can configure globally this style in your app theme using the popupMenuStyle attribute:

  <!-- Base application theme. -->
  <style name="AppTheme" parent="Theme.MaterialComponents.Light">
     ....
    <item name="popupMenuStyle">@style/MyPopup</item>
  </style>

Just use:

PopupMenu popup = new PopupMenu(this, anchor);
popup.getMenuInflater().inflate(R.menu.custom_menu, popup.getMenu());
popup.show();

Currently PopupMenu uses colorOnPrimary to define the textColor. To change it you have to define a textAppearance:

  <style name="MytextAppearanceLargePopupMenu" parent="@style/TextAppearance.MaterialComponents.Subtitle1">
    <item name="android:textColor">@color/colorAccent</item>
  </style>

and then in your app theme adding this attribute:

  <!-- Base application theme. -->
  <style name="AppTheme" parent="Theme.MaterialComponents.Light">
    <item name="textAppearanceLargePopupMenu">@style/MytextAppearanceLargePopupMenu</item>
  </style>



回答2:


Add popupMenu style to ur AppTheme:

<style name="AppTheme" parent="android:Theme.Light">
    <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>

Now in your manifest file

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme">
.............
</application>


来源:https://stackoverflow.com/questions/57712583/popup-menu-background-color-change-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!