Android 更改操作栏上的背景色弹出菜单

Android 更改操作栏上的背景色弹出菜单,android,android-actionbar,popupmenu,Android,Android Actionbar,Popupmenu,我仍然没有找到更改弹出菜单背景色的正确解决方案。但在本例中,我使用AppTheme.NoActionBar是出于特殊原因。这是我的风格 <style name="DarkThemeNoAppBar" parent="AppTheme.NoActionBar"> <item name="backgroundcolor">#3d3b3b</item> <item name="cardbackground">#5e5b5b

我仍然没有找到更改弹出菜单背景色的正确解决方案。但在本例中,我使用AppTheme.NoActionBar是出于特殊原因。这是我的风格

<style name="DarkThemeNoAppBar" parent="AppTheme.NoActionBar">
        <item name="backgroundcolor">#3d3b3b</item>
        <item name="cardbackground">#5e5b5b</item>
        <item name="textcolor">#ede7e7</item>
        <item name="textcolordrawer">#faf6f6</item>
        <item name="tintcolor">#d9d4d4</item>
        <item name="buttoncolor">#45b6ac</item>
        <item name="texttitlecolor">#ede7e7</item>
        <item name="backgroundcolorlayout">#181818</item>
        <item name="android:itemBackground">#181818</item>
        <item name="android:textColor">#ede7e7</item>
        <item name="popupMenuStyle">@style/popupMenuDarkStyle</item>
    </style>


    <style name="popupMenuDarkStyle" parent="Widget.AppCompat.PopupMenu">
        <item name="android:popupBackground">#181818</item>
    </style>
我的弹出菜单附在optionMenu(汉堡按钮)上,这里是如何在Activity中创建optionMenu的

 @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Manual add menu
        menu.add(0, 1, 1, DesignUtil.menuIconWithText(getResources()
                .getDrawable(R.drawable.ic_bookmark_black), "Bookmark"));

        menu.add(0, 2, 1, DesignUtil.menuIconWithText(getResources()
                .getDrawable(R.drawable.ic_settings_black_24dp), "Settings"));

        return true;
    }
是的,弹出菜单显示正确,但背景色仍然无法更改

这对我来说很有效:

<style name="DarkThemeNoAppBar" parent="AppTheme.NoActionBar">
    <item name="backgroundcolor">#3d3b3b</item>
    <item name="cardbackground">#5e5b5b</item>
    <item name="textcolor">#ede7e7</item>
    <item name="textcolordrawer">#faf6f6</item>
    <item name="tintcolor">#d9d4d4</item>
    <item name="buttoncolor">#45b6ac</item>
    <item name="texttitlecolor">#ede7e7</item>
    <item name="backgroundcolorlayout">#181818</item>
    <item name="android:itemBackground">#181818</item>
    <item name="android:textColor">#ede7e7</item>
<!--The "android:" prefix is for a regular widget, such as android.widget.PopupMenu,
while the ones without this prefix are for the support library versions of these widgets-->
    <item name="popupMenuStyle">@style/popupMenuDarkStyle</item>
    <item name="android:popupMenuStyle">@style/popupMenuDarkStyle</item>
</style>


<style name="popupMenuDarkStyle" parent="ThemeOverlay.AppCompat.Light">
    <item name="android:popupBackground">#181818</item>
</style>

#3d3b3b
#5e5b5b
#ede7e7
#faf6f6
#d9d4d4
#45b6ac
#ede7e7
#181818
#181818
#ede7e7
@样式/弹出菜单样式
@样式/弹出菜单样式
#181818
使用
ThemeOverlay.AppCompat.Light
作为父级可能没有必要,但我将其包括在内,因为这就是我的设置和工作方式。关键可能是
android:popupMenuStyle
而不是
popupMenuStyle

这对我很有用:

<style name="DarkThemeNoAppBar" parent="AppTheme.NoActionBar">
    <item name="backgroundcolor">#3d3b3b</item>
    <item name="cardbackground">#5e5b5b</item>
    <item name="textcolor">#ede7e7</item>
    <item name="textcolordrawer">#faf6f6</item>
    <item name="tintcolor">#d9d4d4</item>
    <item name="buttoncolor">#45b6ac</item>
    <item name="texttitlecolor">#ede7e7</item>
    <item name="backgroundcolorlayout">#181818</item>
    <item name="android:itemBackground">#181818</item>
    <item name="android:textColor">#ede7e7</item>
<!--The "android:" prefix is for a regular widget, such as android.widget.PopupMenu,
while the ones without this prefix are for the support library versions of these widgets-->
    <item name="popupMenuStyle">@style/popupMenuDarkStyle</item>
    <item name="android:popupMenuStyle">@style/popupMenuDarkStyle</item>
</style>


<style name="popupMenuDarkStyle" parent="ThemeOverlay.AppCompat.Light">
    <item name="android:popupBackground">#181818</item>
</style>

#3d3b3b
#5e5b5b
#ede7e7
#faf6f6
#d9d4d4
#45b6ac
#ede7e7
#181818
#181818
#ede7e7
@样式/弹出菜单样式
@样式/弹出菜单样式
#181818

使用
ThemeOverlay.AppCompat.Light
作为父级可能没有必要,但我将其包括在内,因为这就是我的设置和工作方式。关键可能是
android:popupMenuStyle
而不是
popupMenuStyle

试试这个,它适合我

步骤1.创建新样式

 <style name="popupMenuStyle" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:textColor">#2FAC7E</item>
    <item name="android:textSize">18sp</item>
    <item name="android:itemBackground">#181818</item>
</style>

试试这个,它对我有用

步骤1.创建新样式

 <style name="popupMenuStyle" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:textColor">#2FAC7E</item>
    <item name="android:textSize">18sp</item>
    <item name="android:itemBackground">#181818</item>
</style>

当我使用onCreateOptionMenu创建菜单时,它对我很有用,试试这个

 <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:itemBackground">#181818</item>
    <item name="android:textColor">@color/colorAccent</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="windowActionBar">false</item>
</style>

#181818
@颜色/颜色重音
真的
@颜色/原色
@颜色/原色暗
@颜色/颜色重音
假的

当我使用onCreateOptionMenu创建菜单时,它对我有效,请尝试以下操作

 <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:itemBackground">#181818</item>
    <item name="android:textColor">@color/colorAccent</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="windowActionBar">false</item>
</style>

#181818
@颜色/颜色重音
真的
@颜色/原色
@颜色/原色暗
@颜色/颜色重音
假的

对不起,我还没上班。我根据你的想法更新了我写的问题,但仍然不适合我。我根据你的想法更新了我写的问题事实上我使用OnCreateOptionMenu创建了菜单我更新了我的问题,你对我的代码条件有什么想法请检查另一个答案它是否可以帮助你@NandaZi我想我接受这个答案,因为我将代码更改为created PopUpMenu。我不再使用CreateOptions菜单上的
,因此它将由绑定在MyAppBar上的ImageView触发,并且可以正常工作。感谢我使用onCreateOptionMenui创建了菜单。我更新了我的问题,你对我的代码条件有什么想法吗?请检查另一个答案。它可以帮助你@NandaZi。我想我接受这个答案,因为我将代码更改为created PopUpMenu。我不再使用CreateOptions菜单上的
,因此它将由绑定在MyAppBar上的ImageView触发,并且可以正常工作。感谢设置主题本身会影响整个应用程序中的文本。如果你只想更改弹出菜单中文本的颜色,这不是一个可行的方法。在主题本身上设置
android:textColor
,会影响整个应用程序中的文本。如果您只想更改弹出菜单中文本的颜色,那么这不是一种可行的方法。