Android - material 设计 - 如何设置上下文菜单的背景颜色

Android - material design - how to set the background color for a context menu

对于列表视图,我有一个上下文菜单(在项目上)。如何设置(通过样式)上下文菜单的背景颜色?

在示例中,我使用了以下基本主题:

<style name="Base.Theme.Xyz" parent="Theme.AppCompat.NoActionBar">

只需按照以下步骤操作:

If by context menu you mean the menu from the long press, then I have done this with the following code. My menu has my theme's background, and a green highlight.

context menu layout:

<menu
  xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/resetConfirm" android:title="@string/actual_reset"></item>
</menu>

styles.xml, where I'm using a custom theme (which I think is the key)

 <style name="GradientLight" parent="@android:style/Theme.Light">
    <item name="android:windowBackground">@drawable/background</item>
    <item name="android:progressBarStyle">@style/progressBar</item>
    <item name="android:buttonStyle">@style/greenButton</item>
    <item name="android:buttonStyleSmall">@style/greenButton</item>
    <item name="android:listViewStyle">@style/listView</item>
    <item name="android:itemBackground">@drawable/menu_selector</item>
    <item name="android:spinnerStyle">@style/spinner</item>
</style>
<style name="listView" parent="@android:style/Widget.ListView.White">
 <item name="android:background">@drawable/background</item>
 <item name="android:listSelector">@drawable/list_selector_background_green</item>
</style>

From: Override context menu colors in Android

检查此 post 以获得另一种可能的解决方案。

希望对您有所帮助

虽然我很感谢前面的回答,但我找到了完美且非常简单的解决方案。

在我的项目中,我使用了这个父级:

<style name="Base.Theme.Deholtmans" parent="Theme.AppCompat.NoActionBar">

我得到了非常暗的上下文菜单等。在 AppCompat 之前,我使用了主题的 Light 版本。

解决方案是使用正确的 pre-defined 父级:

<style name="Base.Theme.Deholtmans" parent="Theme.AppCompat.Light.NoActionBar">

那么,精简版。很简单!