弹出菜单背景颜色更改不起作用 [英] popup menu background color change not working

本文介绍了弹出菜单背景颜色更改不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是android的初学者.我想弹出菜单背景颜色更改.我尝试使用此代码更改背景颜色,但不适用于我.我不知道为什么不使用此代码.

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

推荐答案

您可以使用 android:popupBackground 属性来自定义背景色.

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>

您可以使用

只需使用:

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

当前PopupMenu使用 colorOnPrimary 定义 textColor .要更改它,您必须定义一个textAppearance:

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>

这篇关于弹出菜单背景颜色更改不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆