在 Android 中更改背景 popupMenu

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22320663/
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 05:49:41  来源:igfitidea点击:

Change background popupMenu in Android

androidandroid-themeandroid-stylesandroid-popupwindow

提问by user1854307

I tried to change background of popupmenu, but my implementation does not work.

我试图改变弹出菜单的背景,但我的实现不起作用。

This is my code:

这是我的代码:

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

Apply in AndroidManifest.xml

在 AndroidManifest.xml 中应用

<application
        android:hardwareAccelerated="true"
        android:label="@string/app_name"
        android:icon="@drawable/ic_launcher"
        android:theme="@style/MyHoloLight">

回答by Raju

the following styles working perfectly for me.

以下样式非常适合我。

<style name="popupMenuStyle" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:textColor">@color/color_white</item>
    <item name="android:itemBackground">@color/color_red</item>
</style>

here, parent should be the AppThemeparent

在这里, parent 应该是AppTheme 的父级

and in your code use these lines.

并在您的代码中使用这些行。

Context wrapper = new ContextThemeWrapper(context, R.style.popupMenuStyle);
PopupMenu popup = new PopupMenu(wrapper, v);

i hope it will work.

我希望它会起作用。

回答by Piyush

If bgPopumMenuis your image then use this.

如果bgPopumMenu是您的图像,则使用它。

<style name="popupMenuStyle" parent="@android:style/Widget.PopupMenu">
<item name="android:popupBackground">@drawable/bgPopumMenu</item>
</style>

You need to apply your style to your AppTheme. So try this.

您需要将您的风格应用到您的 AppTheme。所以试试这个。

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

回答by varotariya vajsi

I made some changes of @Raju`s code and the following styles working for me,

我对@Raju 的代码和以下对我有用的样式做了一些更改,

Context wrapper = new ContextThemeWrapper(context,R.style.popupMenuStyle);
PopupMenu popup = new PopupMenu(wrapper, YourView);

and this is my style,

这是我的风格,

<style name="popupMenuStyle" parent="android:Theme.Holo.Light.DarkActionBar">
    <item name="android:popupMenuStyle">@style/MyApp.PopupMenu</item>
    <item name="android:textColor">@color/White</item>
</style>

<style name="MyApp.PopupMenu" parent="android:Widget.Holo.Light.ListPopupWindow">
<item name="android:popupBackground">@color/color_semi_transparent</item>       
</style>

回答by Ali Akram

I SOLVED it,

我解决了,

PopupMenu popup = new PopupMenu(context, view);

PopupMenu popup = new PopupMenu(context, view);

In above code I used context of type Activitynot of Contexttype.

在上面的代码中,我使用了类型Activity不是Context类型的上下文。

Chill !

寒意 !

回答by Heena Arora

If you are using custom theme :

如果您使用自定义主题:

  1. In Manifest : @style/MyMaterialThemeis my customized theme :

      <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:supportsRtl="true"
            android:theme="@style/MyMaterialTheme">
    
  2. Use this code : R.style.popupMenuStylefor popup menu in java class :

  1. 在 Manifest 中:@style/MyMaterialTheme是我定制的主题:

      <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:supportsRtl="true"
            android:theme="@style/MyMaterialTheme">
    
  2. 使用此代码:R.style.popupMenuStyle用于 java 类中的弹出菜单:

Context wrapper = new ContextThemeWrapper(getContext(), R.style.popupMenuStyle);
PopupMenu popup = new PopupMenu(wrapper, v);

//Inflating the Popup using xml file
popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());

MenuPopupHelper menuHelper = new MenuPopupHelper(wrapper, (MenuBuilder) popup.getMenu(), v);
menuHelper.setForceShowIcon(true);
menuHelper.setGravity(Gravity.RIGHT);
  1. this is custom style of popup- menu for your own background drawable
  1. 这是您自己的可绘制背景的自定义弹出菜单样式

<style name="popupMenuStyle" parent="@android:style/Widget.PopupMenu">
    <item name="android:popupBackground">@drawable/dropdown_bg</item>
</style>
  1. then change in your own theme "popupMenuStyle"

    <style name="MyMaterialTheme" parent="MyMaterialTheme.Base">
    
        </style>
    
        <style name="MyMaterialTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
            <item name="windowNoTitle">true</item>
            <item name="windowActionBar">false</item>
            <item name="colorPrimary">@color/colorPrimary</item>
            <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
            <item name="colorAccent">@color/colorPrimary</item>
    
            <item name="popupMenuStyle">@style/popupMenuStyle</item>
            <item name="android:popupMenuStyle">@style/popupMenuStyle</item>
        </style>
    
  1. 然后更改您自己的主题“popupMenuStyle”

    <style name="MyMaterialTheme" parent="MyMaterialTheme.Base">
    
        </style>
    
        <style name="MyMaterialTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
            <item name="windowNoTitle">true</item>
            <item name="windowActionBar">false</item>
            <item name="colorPrimary">@color/colorPrimary</item>
            <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
            <item name="colorAccent">@color/colorPrimary</item>
    
            <item name="popupMenuStyle">@style/popupMenuStyle</item>
            <item name="android:popupMenuStyle">@style/popupMenuStyle</item>
        </style>
    

回答by Manoranjan

You can easily create a style for popup menu and apply that style to a theme and you can assign the theme to the Activity/Parent Activity*** in the android manifest like this

您可以轻松地为弹出菜单创建样式并将该样式应用于主题,您可以像这样将主题分配给 android manifest 中的 Activity/Parent Activity***

<style name="MyThemePopup" parent="@style/AppTheme.NoActionBar">
    <item name="android:popupMenuStyle">@style/PopupMenu</item>
</style>

Create @style/PopupMenu like this

像这样创建@style/PopupMenu

<style name="PopupMenu" parent="@android:style/Widget.PopupMenu">
    <item name="android:popupBackground">#000000</item>
</style>

Assign the MyThemePopup theme to your Activity/Parent Activity*** in AndroidManifest as

在 AndroidManifest 中将 MyThemePopup 主题分配给您的活动/父活动***

<activity
            android:name=".Activity"
            android:label="@string/title_activity"
            android:screenOrientation="portrait"
            android:theme="@style/MyThemePopup"/>

***When you using the fragment apply the theme for the parent activity defined in AndroidManifest

***当您使用片段时,请为 AndroidManifest 中定义的父活动应用主题

回答by axita.savani

just add below line into Style.xml and its working :

只需将以下行添加到 Style.xml 及其工作中:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- To change action bar menuItem BG -->
            <item name="android:itemBackground">@color/white</item>
 </style>

回答by Lucian Novac

what about this :

那这个呢 :

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    MenuInflater inflater=getMenuInflater();
    inflater.inflate(R.menu.menu,menu);
    setMenuBackground(); 
    return true;    
}

and write this in the setMenuBackground()method

并在setMenuBackground()方法中 写下这个

protected void setMenuBackground(){                              
        getLayoutInflater().setFactory( new Factory() {  
            public View onCreateView(String name, Context context, AttributeSet attrs) {
                if ( name.equalsIgnoreCase( "com.android.internal.view.menu.IconMenuItemView" ) ) {
                    try { // Ask our inflater to create the view  
                        LayoutInflater f = getLayoutInflater();  
                        final View view = f.createView( name, null, attrs );  
                        /* The background gets refreshed each time a new item is added the options menu.  
                        * So each time Android applies the default background we need to set our own  
                        * background. This is done using a thread giving the background change as runnable 
                        * object */
                        new Handler().post( new Runnable() {  
                            public void run () {  
                                // sets the background here
                                view.setBackgroundResource( R.drawable.bgPopumMenu);
                                // sets the text color              
                                ((TextView) view).setTextColor(Color.BLACK);
                                // sets the text size              
                                ((TextView) view).setTextSize(18);
                }
                        } );  
                    return view;
                }
            catch ( InflateException e ) {}
            catch ( ClassNotFoundException e ) {}  
        } 
        return null;
    }}); 
}

回答by tasomaniac

You cannot give android:popupBackgroundas just a color. You should use or create a drawable.

你不能仅仅给出android:popupBackground一种颜色。您应该使用或创建一个drawable.

You can use this link http://jgilfelt.github.io/android-actionbarstylegenerator/to generate your desired color. And set it to that drawable.

您可以使用此链接http://jgilfelt.github.io/android-actionbarstylegenerator/生成您想要的颜色。并将其设置为drawable.

回答by paaacman

Try to define android:actionBarWidgetThemein your theme:

尝试android:actionBarWidgetTheme在您的主题中定义:

<style name="MyHoloLight" parent="android:Theme.Holo.Light">
    <item name="android:popupMenuStyle">@style/popupMenuStyle</item>
    <item name="android:actionBarWidgetTheme">@style/Theme.Example.Widget</item>
</style>
<style name="popupMenuStyle" parent="@android:style/Widget.PopupMenu">
    <item name="android:popupBackground">@color/bgPopumMenu</item>
</style>
<style name="Theme.Example.Widget" parent="@style/Theme.AppCompat">
    <item name="popupMenuStyle">@style/popupMenuStyle</item>
    <item name="dropDownListViewStyle">@style/DropDownListView.Example</item>
</style>
<style name="DropDownListView.Example" parent="@style/Widget.AppCompat.ListView.DropDown">
    <item name="android:listSelector">@color/bgPopumMenu_whenSelected</item>
</style>