将样式更改为 MaterialComponents 后,溢出菜单背景颜色消失

Tra*_*Nam 4 xml android android-theme material-components material-components-android

起初,我的应用程序使用<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">样式。但是我将其更改为 Material Components <style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">(为了使用标签的材质徽章计数)。所以改变后的样式是这样的:

<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/green</item>
    <item name="colorPrimaryDark">@color/green</item>
    <item name="colorAccent">@color/green</item>
</style>
Run Code Online (Sandbox Code Playgroud)

更改样式后,溢出菜单背景现在为白色。(之前是绿底白字Theme.AppCompat.Light.DarkActionBar)。但是现在背景是白色的,文字也是白色的。

这是工具栏的xml:

<androidx.appcompat.widget.Toolbar
    android:id="@+id/ev_toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/green"
    android:minHeight="?attr/actionBarSize"
    android:theme="@style/ToolbarTheme">
</androidx.appcompat.widget.Toolbar>
Run Code Online (Sandbox Code Playgroud)

和工具栏的主题样式:

<style name="ToolbarTheme" parent="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    <item name="android:textColorPrimary">@color/white</item>
    <item name="android:colorBackground">@color/green</item>
</style>
Run Code Online (Sandbox Code Playgroud)

我尝试<item name="popupMenuStyle">@style/CustomPopup</item>AppThemewith 中设置

<style name="CustomPopup" parent="@style/Widget.MaterialComponents.PopupMenu">
    <item name="android:popupBackground">@color/green</item>
</style>
Run Code Online (Sandbox Code Playgroud)

但仍然没有运气。

我用来测试的设备使用的是 Android 8.0,compileSdkVersion并且targetSdkVersion是 28。

感谢您的时间。

Gab*_*tti 5

溢出菜单中弹出窗口的背景颜色在应用程序主题中由属性定义actionOverflowMenuStyle

<style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
   <item name="actionOverflowMenuStyle">@style/Widget.MaterialComponents.PopupMenu.Overflow</item>
</style>
Run Code Online (Sandbox Code Playgroud)

所述1.2.0-alpha02 固定此值之间明暗主题的混合。

您还可以使用以下方法自定义弹出窗口:

    <com.google.android.material.appbar.MaterialToolbar
        app:popupTheme="@style/AppTheme.PopupOverlay"
        ../>
Run Code Online (Sandbox Code Playgroud)

和:

  <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.MaterialComponents.Toolbar.Primary">
    <item name="android:popupBackground">@drawable/...</item>
  </style>
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

您也可以覆盖颜色不改变使用的绘制android:theme中的属性Toolbar

    <com.google.android.material.appbar.MaterialToolbar
        android:theme="@style/MyThemeOverlay_Toolbar"
        ../>
Run Code Online (Sandbox Code Playgroud)

使用:

  <style name="MyThemeOverlay_Toolbar" parent="ThemeOverlay.MaterialComponents.Toolbar.Primary">
    <item name="colorSurface">@color/custom</item>
  </style>
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明