在Android中更改背景popupMenu

问题描述 投票:11回答:11

我试图改变popupmenu的背景,但我的实现不起作用。

这是我的代码:

<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">
android android-theme android-styles android-popupwindow
11个回答
26
投票

以下款式适合我。

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

我希望它能奏效。


0
投票

您可以轻松地为弹出菜单创建样式并将该样式应用于主题,您可以将主题分配给Android清单中的Activity / Parent Activity ***,如下所示

    <item name="android:panelBackground">@drawable/bgPopumMenu</item>

像这样创建@ style / PopupMenu

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

将MyThemePopup主题分配给AndroidManifest中的Activity / Parent Activity ***

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

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


0
投票

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

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

5
投票

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

3
投票

我对@ 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>

1
投票

如果您使用自定义主题:

  1. 在Manifest中:qazxsw poi是我的自定义主题: qazxsw poi
  2. 使用以下代码:@style/MyMaterialTheme用于java类中的弹出菜单:

  <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/MyMaterialTheme">
  1. 这是自定义样式的弹出菜单,适合您自己的背景绘制

R.style.popupMenuStyle
  1. 然后改变你自己的主题“popupMenuStyle” 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
投票

我解决了,

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

在上面的代码中,我使用了<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> 类型的上下文而不是PopupMenu popup = new PopupMenu(context, view);类型。

寒冷!


0
投票

那这个呢 :

Activity

并用Context方法写出来

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

0
投票

你不能只把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; }}); }

您可以使用此链接android:popupBackground生成所需的颜色。然后把它设置为drawable


0
投票

0
投票

如果bgPopumMenu是你的drawable,那么使用它

android:actionBarWidgetTheme

你只需将它直接放入你的AppTheme就好了

<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>
© www.soinside.com 2019 - 2024. All rights reserved.