如何更改背景颜色弹出菜单android

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21231404/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:10:42  来源:igfitidea点击:

How to change background color popup menu android

androidmenucolorspopup

提问by user3172337

I generate each option of menu from querying database. Here is my code.

我从查询数据库中生成菜单的每个选项。这是我的代码。

final PopupMenu popupMenu = new PopupMenu(getBaseContext(), v); 
SQLiteDatabase db = AdapterDb.getReadableDatabase(); 
Cursor cursor = db.rawQuery(sql, null);
int ctritem = 0;
if (cursor.moveToFirst()) {
    popupMenu.getMenu().add(Menu.NONE, ctritem, Menu.NONE, "ALL ITEMS"); 
    do {
        ctritem++;   
        popupMenu.getMenu().add(Menu.NONE, ctritem, Menu.NONE, cursor.getString(0)); 
    } while (cursor.moveToNext());
}

Everything is okay, but the problem is how to change color of option menu or background color of popup menu (from black to white), Is it possible ? Thanks

一切正常,但问题是如何更改选项菜单的颜色或弹出菜单的背景颜色(从黑色到白色),这可能吗?谢谢

回答by user543

Add popupMenu style to ur AppTheme:

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

manifest.xml:

清单.xml:

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

回答by MohanRaj S

Please Add Following lines in style xml file, I hope this will help for material design application. Style.xml

请在样式 xml 文件中添加以下行,我希望这对材料设计应用程序有帮助。 样式文件

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>

    <!-- To change the popup menu and app text color  -->
    <item name="android:textColor">@color/colorPrimary</item>

    <!-- To change the background of options menu-->
    <item name="android:itemBackground">@color/skyBlue</item>
</style>

For more details refer this link http://www.viralandroid.com/2016/01/how-to-change-background-and-text-color-of-android-actionbar-option-menu.html

有关更多详细信息,请参阅此链接http://www.viralandroid.com/2016/01/how-to-change-background-and-text-color-of-android-actionbar-option-menu.html

回答by Kalpesh

If your are using AppCompact-v7 then you can style PopupMenu as below:

如果您使用的是 AppCompact-v7,那么您可以设置如下样式的 PopupMenu:

 <style name="AppTheme" parent="Theme.AppCompat.Light">
    <item name="popupMenuStyle">@style/popupMenuStyle</item>
</style>

<style name="popupMenuStyle" parent="Widget.AppCompat.PopupMenu">
    <item name="android:popupBackground">@android:color/white</item>
</style>

NOTE: PopMenu always works with Activity Context, not with Application context.

注意:PopMenu 始终适用于活动上下文,而不适用于应用程序上下文。

回答by user3024215

if you are using AppCompat theme then use

如果您使用的是 AppCompat 主题,请使用

<style name="PopupMenu" parent="Widget.AppCompat.PopupMenu">
    <item name="android:popupBackground">@android:color/black</item>

</style>

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="popupMenuStyle">@style/PopupMenu</item>
</style>

otherwise

除此以外

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

回答by Mohammad tanvirul islam

at first define your style for popup menu background in styles.xml file in my case i use this...

首先在styles.xml文件中定义弹出菜单背景的样式,在我的情况下我使用这个......

<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
    <item name="android:popupMenuStyle">@style/PopupMenu</item>
</style>
<style name="PopupMenu" parent="@android:style/Widget.PopupMenu">
    <item name="android:popupBackground">@android:color/holo_green_light</item>
</style>

here i want to change popup menu background in a specific activity , so apply theme(android:theme="@style/AppTheme") with activity declaration and it's surely working. another important thing to create popup menu using this code PopupMenu popup = new PopupMenu(your activity, viewObj);

在这里,我想更改特定活动中的弹出菜单背景,因此将 theme(android:theme="@style/AppTheme") 与活动声明一起应用,它肯定会起作用。使用此代码创建弹出菜单的另一件重要事情 PopupMenu popup = new PopupMenu(your activity, viewObj);

回答by user2118561

In the layout where you are giving toolbar, specify theme by app:popupTheme="@style/MyPopupMenu"

在您提供工具栏的布局中,通过 app:popupTheme="@style/MyPopupMenu" 指定主题

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:minHeight="?attr/actionBarSize"
    android:background="#2196F3"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    style="@style/DrawerArrowStyle"
    app:popupTheme="@style/MyPopupMenu">

</android.support.v7.widget.Toolbar>

回答by Droid_Mechanic

In StyleYour Application Theme

风格化您的应用程序主题

 <style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
     <!-- Customize your theme here. -->
 <item name="android:itemBackground">@color/list_background</item>
 <item name="android:itemTextAppearance">@style/MyActionBar.MenuTextStyle</item>
    </style>

For Text Appearence

对于文本外观

 <style name="MyActionBar" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
        <item name="background">@color/list_item_title</item>
        <item name="titleTextStyle">@style/MyActionBarTitle</item>
    </style>
    <style name="MyActionBarTitle" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
        <item name="android:textColor">@color/list_item_title</item>
    </style>

    <style name="MyActionBar.MenuTextStyle"
        parent="style/TextAppearance.AppCompat.Widget.ActionBar.Title">
        <item name="android:textColor">@color/list_item_title</item>
        <item name="android:textStyle">normal</item>
        <item name="android:textSize">16sp</item>
        <item name="android:gravity">center</item>
    </style>

And Use App Theme In Menifest like:

并在 Menifest 中使用应用程序主题,例如:

<application
        android:name="Your Package"
        android:allowBackup="true"
        android:icon="@drawable/launcher_icon"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

回答by M. Doosti Lakhani

Add PopupMenu theme to your AppTheme:

将 PopupMenu 主题添加到您的 AppTheme:

<style name="AppTheme" parent="Theme.AppCompat.Light">
    <item name="popupTheme">@style/PopupMenuTheme</item>
</style>

<style name="PopupMenuTheme" parent="Theme.AppCompat.Light">
    <item name="android:background">@android:color/white</item>
</style>

manifest.xml:

清单.xml:

<application
    ...
    android:theme="@style/AppTheme">
    ...
</application>

回答by Vaibhav Gupta

Please Refer How to style PopupMenu?for more explanation. I was facing the same issue, searched alot but found the solution in the link mentioned.

请参阅如何设置 PopupMenu 的样式?更多解释。我遇到了同样的问题,搜索了很多,但在提到的链接中找到了解决方案。

PopupMenu is created in following way :

PopupMenu 的创建方式如下:

PopupMenu popup = new PopupMenu(context, view);

PopupMenu takes the styling of the context which is passed, Passing Activity as the context solved my problem.

PopupMenu 采用传递的上下文的样式,传递 Activity 作为上下文解决了我的问题。

回答by sathish mobapp dev

<style name="AppTheme1" parent= "android:Theme.DeviceDefault">

    <item name="android:actionBarPopupTheme">@style/popupNew</item>
</style>

<style name="popupNew" parent="android:ThemeOverlay.Material.Light">
    <item name="android:colorBackground">#ffffff</item>
</style>