猿问

如何设置PopupMenu的样式?

是否可以将弹出菜单样式从白色背景上的默认黑色文本更改为深色背景,而无需将样式应用于整个活动(这会破坏UI)?



摇曳的蔷薇
浏览 1280回答 3
3回答

慕标5832272

是的你可以<style name="YOURSTYLE" parent="Widget.AppCompat.PopupMenu">&nbsp; &nbsp; <item name="android:textColor">@android:color/white</item>&nbsp; &nbsp; <item name="android:itemBackground">@android:color/holo_red_light</item></style>和Context wrapper = new ContextThemeWrapper(this, R.style.YOURSTYLE);PopupMenu popup = new PopupMenu(wrapper, view);

隔江千里

您不能PopupMenu直接设置样式,但是还有其他方法。PopupMenu 是通过以下方式创建的:PopupMenu popupMenu=new PopupMenu(context, anchorView);菜单的样式由您传递的上下文样式决定。因此,您要做的就是将您的Activity引用作为上下文传递,并且菜单将相应地设置样式。如果要自己定义样式,请从默认样式之一继承您的活动样式,并覆盖以下各项:<style name="style" parent="android:Theme.Holo.Light">&nbsp; &nbsp; <item name="android:popupMenuStyle">...</item>&nbsp; &nbsp; <item name="android:popupAnimationStyle">...</item>&nbsp; &nbsp; <item name="android:popupBackground">...</item>&nbsp; &nbsp; <!-- etc etc --></style>

红糖糍粑

除了Deville建议的内容外,您还可以在主题样式中添加以下属性。<style name="style" parent="android:Theme.Holo.Light">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; <!-- other attributes -->&nbsp; &nbsp; <item name="textAppearanceLargePopupMenu">@style/myPopupMenuTextAppearanceLarge</item>&nbsp; &nbsp; <item name="android:textAppearanceLargePopupMenu">@style/myPopupMenuTextAppearanceLarge</item>&nbsp; &nbsp; <item name="textAppearanceSmallPopupMenu">@style/myPopupMenuTextAppearanceSmall</item>&nbsp; &nbsp; <item name="android:textAppearanceSmallPopupMenu">@style/myPopupMenuTextAppearanceSmall</item>&nbsp; &nbsp; <item name="popupMenuStyle">@style/myPopupMenuStyle</item>&nbsp; &nbsp; <item name="android:popupMenuStyle">@style/myPopupMenuStyle</item></style>上述样式定义中引用的其他样式<style name="myPopupMenuStyle" parent="@style/Widget.AppCompat.Light.PopupMenu"></style><style name="myPopupMenuTextAppearanceSmall" parent="@style/TextAppearance.AppCompat.Light.Widget.PopupMenu.Small">&nbsp; &nbsp; <item name="android:textColor">#000000</item></style><style name="myPopupMenuTextAppearanceLarge" parent="@style/TextAppearance.AppCompat.Light.Widget.PopupMenu.Large">&nbsp; &nbsp; <item name="android:textColor">#000000</item></style>您会在我的xml样式定义中注意到AppCompat,这是因为我正在使用android支持库来定位较低的android API级别。
随时随地看视频慕课网APP

相关分类

Android
我要回答