在安卓中更改弹出菜单的背景

13

我尝试修改弹出菜单的背景,但是我的实现不起作用。

这是我的代码:

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

在 AndroidManifest.xml 中应用

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

32

以下样式对我完美地起作用。

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

这里,父元素应该是AppTheme

在你的代码中使用下面这些行。

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

我希望它会起作用。


这可能是唯一一个可以无需任何麻烦就能正常工作的答案。致敬! - Suhayl SH
我花了很多天的时间尝试让菜单颜色正常工作。这个解决方案是唯一完美运行的。我正在使用AppCompact和MenuPopupHelper。感谢@Raju! - Lisitso

7
如果 bgPopumMenu 是你的图片,则使用以下代码。
<style name="popupMenuStyle" parent="@android:style/Widget.PopupMenu">
<item name="android:popupBackground">@drawable/bgPopumMenu</item>
</style>

您需要将样式应用到您的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>

bgPopupMenu是你的图像还是颜色名称? - Piyush
bgPopupMenu - 这个颜色。 - user1854307
你为它创建了颜色XML文件吗? - Piyush
在 XML 文件中创建的颜色。 - user1854307
我尝试使用drawable来使用bgPopupMenu,但这并没有帮助。 - user1854307
显示剩余3条评论

6
我对@Raju的代码进行了一些修改,以下样式适用于我:
Context wrapper = new ContextThemeWrapper(context,R.style.popupMenuStyle);
PopupMenu popup = new PopupMenu(wrapper, YourView);

这是我的风格:
<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>

2

如果您正在使用自定义主题:

  1. In Manifest : @style/MyMaterialTheme is 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.popupMenuStyle for popup menu in java class :

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

<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

我解决了它,

PopupMenu popup = new PopupMenu(context, view);

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

放松!


0
如果 bgPopumMenu 是你的可绘制对象,则使用此代码:
    <item name="android:panelBackground">@drawable/bgPopumMenu</item>

你只需将其直接放入你的AppTheme中,就像这样

<style name="AppTheme" parent="android:Theme.Holo.Light">
    <item name="android:panelBackground">@drawable/bgPopumMenu</item>
</style>

0
尝试在您的主题中定义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>

0

只需将以下行添加到Style.xml中即可:

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

这将仅更改项目的背景,而不是整个菜单的背景,因此在执行此操作后,顶部和底部仍会留下一些不同颜色的边距。 - Kashish Sharma
你需要移除边距,并且在弹出菜单上工作,所有项目的背景颜色都会改变。此外,你的应用程序必须使用parent="Theme.AppCompat.Light.NoActionBar"这个ActionBar。 - axita.savani

0
这个怎么样?
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    MenuInflater inflater=getMenuInflater();
    inflater.inflate(R.menu.menu,menu);
    setMenuBackground(); 
    return true;    
}

并在 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;
    }}); 
}

错误将会发生: 在此LayoutInflater上已经设置了一个工厂 - Raptor
由于支持库的一个小改变,详见此链接:https://dev59.com/82rXa4cB1Zd3GeqPEO_E - Lucian Novac

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接