我试图改变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">
以下款式适合我。
<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);
我希望它能奏效。
您可以轻松地为弹出菜单创建样式并将该样式应用于主题,您可以将主题分配给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中定义的父活动的主题
只需将以下行添加到Style.xml及其工作中:
<style name="PopupMenu" parent="@android:style/Widget.PopupMenu">
<item name="android:popupBackground">#000000</item>
</style>
如果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>
我对@ 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>
如果您使用自定义主题:
@style/MyMaterialTheme
用于java类中的弹出菜单: <application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/MyMaterialTheme">
R.style.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);
我解决了,
<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);
类型。
寒冷!
那这个呢 :
Activity
并用Context
方法写出来
@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;
}});
}
。
您可以使用此链接android:popupBackground
生成所需的颜色。然后把它设置为drawable
。
尝试在主题中定义http://jgilfelt.github.io/android-actionbarstylegenerator/:
drawable
如果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>