在NoActionBar上更改背景色弹出菜单

时间:2019-03-18 09:54:47

标签: android android-actionbar popupmenu

我仍然没有找到正确的解决方案来更改PopupMenu的背景颜色。但是在这种情况下,出于特殊原因,我使用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>

如果看到popupMenuStyle,则此popupMenuDarkStyle在UI中没有任何作用。谷歌上的许多文献都解决了这个问题,但它仅适用于Theme.AppCompat.Light.DarkActionBar,但就我而言,我需要使用AppTheme.NoActionBar。请有人给我一些建议。

更新2 这是第二次尝试

<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="android:popupMenuStyle">@style/popupMenuDarkStyle</item>
    </style>


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

我的PopupMenu附加在optionMenu(在Burger按钮上)上,这里是在Activity中如何创建optionsMenu。

 @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;
    }

是的,popupmenu可以正确显示,但是背景色仍然无法更改

3 个答案:

答案 0 :(得分:1)

尝试一下,它对我有用

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

第2步。在添加PopUpMenu的活动中添加此行

 Context wrapper = new ContextThemeWrapper(activity, R.style.popupMenuStyle);
 final PopupMenu popup = new PopupMenu(wrapper, view); 

答案 1 :(得分:1)

当我使用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>

答案 2 :(得分:0)

这对我有用:

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

可能不需要使用ThemeOverlay.AppCompat.Light作为父对象,但是我将其包括在内是因为这是我的设置方式和工作方式。密钥可能是android:popupMenuStyle而不是popupMenuStyle