为什么我的PopupMenu没有被设计?

问题描述 投票:0回答:2

在我的应用程序中,我创建一个像这样的PopupMenu

public void openPopup(View v) {
    PopupMenu menu = new PopupMenu(MainActivity.this, v);
    menu.inflate(R.menu.menu_popup);
    menu.show();
}

我将这样的弹出式样式设置为:

<style name="Theme.MyTheme" parent="@style/Theme.AppCompat.Light">
    <item name="popupMenuStyle">@style/PopupMenu.MyTheme</item>
    <item name="android:popupMenuStyle">@style/PopupMenu.MyTheme</item>    
</style>

<style name="PopupMenu.MyTheme" parent="@style/Widget.AppCompat.Light.PopupMenu">
    <item name="android:popupBackground">@color/black</item>
    <item name="android:textColor">@color/white</item>
</style>

我也申请了其他正常的主题

<application
    android:theme="@style/Theme.MyTheme"
    ... >

然而弹出窗口显示白色背景和黑色文本。有什么不对?

android popup android-theme android-styles
2个回答
1
投票

这需要一些挖掘,但我最终想出来了。要么只需要覆盖popupMenuStylepopupWindowStyle。我现在不记得是哪一个。

我不确定它是否适用于AppCompat库,尤其是使用listChoiceBackgroundIndicator。我没有测试任何针对少于14的东西的东西。

<style name="Theme.MyAppTheme" parent="android:Theme.Holo.Light">
<item name="android:popupMenuStyle">@style/MyAwesomeBackground.PopupStyle</item>
<item name="android:popupWindowStyle">@style/MyAwesomeBackground.PopupStyle</item>
<item name="android:textAppearanceLargePopupMenu">@style/CustomTextStyle</item>
<item name="android:textAppearanceSmallPopupMenu">@style/CustomTextStyle</item>
<item name="android:listChoiceBackgroundIndicator">@drawable/custom_list_selector</item>
</style>
<style name="AwesomeBackground.PopupStyle" parent="Widget.PopupMenu">
   <item name="android:popupBackground">@drawable/custom_background</item>
</style>
<style name="CustomTextStyle">
<!-- some custom text stylings -->
</style>

0
投票

在菜单上,将项目设置为:onClick =“showPopUp”,确保存在此类方法。

public void showPopUp(View v)
{
  Context context = new ContextThemeWrapper(this,R.style.MyMaterialTheme);

  PopupMenu popupMenu=new PopupMenu(context,v);

  MenuInflater inflater=popupMenu.getMenuInflater();
  inflater.inflate(R.menu.button_menu,popupMenu.getMenu());
}

在styles.xml中,添加以下内容:

   <style name="MyMaterialTheme"  parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="windowActionBar">false</item>
    <item name="android:textColorPrimary">@color/black</item>
    <item name="android:textStyle">bold</item>
    <item name="android:iconifiedByDefault">true</item>
    <item name="android:backgroundTint">@color/orange</item></style>
© www.soinside.com 2019 - 2024. All rights reserved.