How to change background color popup menu android

夙愿已清 提交于 2019-11-26 09:34:37

问题


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


回答1:


Add popupMenu style to ur 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:

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



回答2:


Please Add Following lines in style xml file, I hope this will help for material design application. Style.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




回答3:


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

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




回答4:


if you are using AppCompat theme then use

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



回答5:


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

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




回答6:


In the layout where you are giving toolbar, specify theme by 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>



回答7:


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 is created in following way :

PopupMenu popup = new PopupMenu(context, view);

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




回答8:


Add PopupMenu theme to your 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:

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



回答9:


In Style Your 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:

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



回答10:


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



回答11:


Add this to your style.xml file

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

Set the style on Spinner

<Spinner
android:theme="@style/PopupMenu"
android:popupMenuStyle="@style/PopupMenu"/>

This would work for API level 16+ devices too.




回答12:


You can use custom layout like this..

PopupMenu pop = new PopupMenu(MainActivity.this, v);
pop.getMenuInflater().inflate(R.menu.main, pop.getMenu());
pop.show();

Design layout how you want..hope this helps..



来源:https://stackoverflow.com/questions/21231404/how-to-change-background-color-popup-menu-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!