在更改Android背景POPUPMENU在更改Android背景POPUPMENU(Change

2019-05-12 05:02发布

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

这是我的代码:

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

Answer 1:

以下样式为我工作完美。

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

我希望它会奏效。



Answer 2:

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


Answer 3:

我做了@ Raju`s代码和一些变化工作对我来说以下样式,

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>


Answer 4:

如果您使用的自定义主题:

  1. 在清单: @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. 这是popup-菜单的自定义样式为自己绘制背景

 <style name="popupMenuStyle" parent="@android:style/Widget.PopupMenu"> <item name="android:popupBackground">@drawable/dropdown_bg</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> 


Answer 5:

我解决了它,

PopupMenu popup = new PopupMenu(context, view);

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

冷静!



Answer 6:

那这个呢 :

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


Answer 7:

你不能给android:popupBackground只是一个颜色。 您应该使用或创建一个drawable

你可以使用这个链接http://jgilfelt.github.io/android-actionbarstylegenerator/来生成所需的颜色。 并且其设置为drawable



Answer 8:

尝试定义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>


Answer 9:

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


Answer 10:

您可以轻松地创建弹出菜单风格和样式应用到一个主题,你可以为主题分配到活动/父活动***在这样的Android清单

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

创建@风格/ PopupMenu的这样

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

分配MyThemePopup主题,以您的活动/父活动***在AndroidManifest作为

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

***当使用片段应用的主题为AndroidManifest定义父活动



Answer 11:

只需添加下面一行到Style.xml及其工作:

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


文章来源: Change background popupMenu in Android