弹出菜单背景颜色更改不起作用

时间:2019-08-29 14:40:43

标签: android material-design android-toolbar material-components-android material-components

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

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

//此活动代码用于弹出菜单

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

2 个答案:

答案 0 :(得分:1)

将popupMenu样式添加到您的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>

现在在清单文件中

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

答案 1 :(得分:0)

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

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

您可以使用popupMenuStyle属性在应用程序主题中全局配置此样式:

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

use

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

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

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

,然后在您的应用程序主题中添加以下属性:

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