Android 如何设置 PopupMenu 的样式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12636101/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:16:07  来源:igfitidea点击:

How to style PopupMenu?

androiduser-interface

提问by Violet Giraffe

is it possible to change pop-up menu style from default black text on white background to dark background without applying style to the whole activity (which breaks my UI)?

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

回答by Frank Nguyen

Yes, you can

是的你可以

<style name="YOURSTYLE" parent="Widget.AppCompat.PopupMenu">
    <item name="android:textColor">@android:color/white</item>
    <item name="android:itemBackground">@android:color/holo_red_light</item>
</style>

And

Context wrapper = new ContextThemeWrapper(this, R.style.YOURSTYLE);
PopupMenu popup = new PopupMenu(wrapper, view);


Result

结果

custom PopupMenu style in Android

Android 中的自定义 PopupMenu 样式

回答by Andrii Chernenko

You cannot set PopupMenustyle directly, but there are other ways.

您不能PopupMenu直接设置样式,但还有其他方法。

PopupMenuis created the following way:

PopupMenu是通过以下方式创建的:

PopupMenu popupMenu=new PopupMenu(context, anchorView);

The style of menu is determined by the style of context you pass. So all you need to do is to pass your Activityreference as context, and menu will be styled accordingly.

菜单的样式由您传递的上下文样式决定。所以你需要做的就是将你的Activity引用作为上下文传递,菜单将相应地设置样式。

If you want to define the style yourself, inherit your activity style from one of the default ones and override the following items:

如果您想自己定义样式,请从默认样式之一继承您的活动样式并覆盖以下项目:

<style name="style" parent="android:Theme.Holo.Light">
    <item name="android:popupMenuStyle">...</item>
    <item name="android:popupAnimationStyle">...</item>
    <item name="android:popupBackground">...</item>
    <!-- etc etc -->
</style>

回答by Jimmy Ilenloa

Adding to what Devillesuggested, you can also add the following attributes to your theme style.

除了Deville 的建议之外,您还可以将以下属性添加到您的主题样式中。

<style name="style" parent="android:Theme.Holo.Light">        
    <!-- other attributes -->
    <item name="textAppearanceLargePopupMenu">@style/myPopupMenuTextAppearanceLarge</item>
    <item name="android:textAppearanceLargePopupMenu">@style/myPopupMenuTextAppearanceLarge</item>

    <item name="textAppearanceSmallPopupMenu">@style/myPopupMenuTextAppearanceSmall</item>
    <item name="android:textAppearanceSmallPopupMenu">@style/myPopupMenuTextAppearanceSmall</item>

    <item name="popupMenuStyle">@style/myPopupMenuStyle</item>
    <item name="android:popupMenuStyle">@style/myPopupMenuStyle</item>
</style>

Other styles referenced in the above style definition

上述样式定义中引用的其他样式

<style name="myPopupMenuStyle" parent="@style/Widget.AppCompat.Light.PopupMenu">

</style>
<style name="myPopupMenuTextAppearanceSmall" parent="@style/TextAppearance.AppCompat.Light.Widget.PopupMenu.Small">
    <item name="android:textColor">#000000</item>
</style>
<style name="myPopupMenuTextAppearanceLarge" parent="@style/TextAppearance.AppCompat.Light.Widget.PopupMenu.Large">
    <item name="android:textColor">#000000</item>
</style>

You would notice AppCompatin my xml style definitions, that is because I am using the android support library to target lower android API Levels.

您会在我的 xml 样式定义中注意到AppCompat,这是因为我使用 android 支持库来定位较低的 android API 级别。

回答by Aritra Roy

You can obviously customize the PopupMenu in your own way. Its quite simple. But first you need to take care of one very important thing.

您显然可以以自己的方式自定义 PopupMenu。它很简单。但首先你需要处理一件非常重要的事情。

The PopupMenu is created like this,

PopupMenu 是这样创建的,

PopupMenu popupMenu = new PopupMenu(context, anchorView);

Now the "context" here plays a very important role in styling. The style of the PopupMenu depends on the style of the context that you pass. So be very careful in this. I wasted almost half-and-hour figuring out this.

现在这里的“上下文”在样式中起着非常重要的作用。PopupMenu 的样式取决于您传递的上下文的样式。所以在这方面要非常小心。我浪费了将近半个小时来弄清楚这一点。

If you are in a fragment just pass "getActivity()" and you are done.

如果您在片段中,只需通过“getActivity()”即可完成。

Styling of the PopupMenu items

PopupMenu 项的样式

Just override the following items in your style,

只需覆盖您风格中的以下项目,

<item name="textAppearanceLargePopupMenu">@style/myPopupMenuTextAppearanceLarge</item>
<item name="android:textAppearanceLargePopupMenu">@style/myPopupMenuTextAppearanceLarge</item>

<item name="textAppearanceSmallPopupMenu">@style/myPopupMenuTextAppearanceSmall</item>
<item name="android:textAppearanceSmallPopupMenu">@style/myPopupMenuTextAppearanceSmall</item>

And customize the text appearance as you want,

并根据需要自定义文本外观,

<style name="myPopupMenuTextAppearanceSmall" parent="@style/TextAppearance.AppCompat.Light.Widget.PopupMenu.Small">
            <item name="android:textColor">@color/text_hint_color</item>
</style>

<style name="myPopupMenuTextAppearanceLarge" parent="@style/TextAppearance.AppCompat.Light.Widget.PopupMenu.Large">
            <item name="android:textColor">@color/text_hint_color</item>
</style>

If you want to change the background of the PopupMenu, do this

如果要更改 PopupMenu 的背景,请执行以下操作

<item name="popupMenuStyle">@style/myPopupMenuStyle</item>
    <item name="android:popupMenuStyle">@style/myPopupMenuStyle</item>

Advanced Styling

高级造型

The PopupMenu items do not support showing the icons by default. But showing the icons can make it look so much better. I recommend trying it.

默认情况下,PopupMenu 项不支持显示图标。但是显示图标可以使它看起来更好。我建议尝试一下。

To implement this just put the following code in your activity and you are good to go,

要实现这一点,只需将以下代码放入您的活动中,您就可以开始了,

@Override
public boolean onMenuOpened(int featureId, Menu menu) {
    if (featureId == Window.FEATURE_ACTION_BAR && menu != null) {
        if (menu.getClass().getSimpleName().equals("MenuBuilder")) {
            try {
                Method m = menu.getClass().getDeclaredMethod(
                        "setOptionalIconsVisible", Boolean.TYPE);
                m.setAccessible(true);
                m.invoke(menu, true);
            } catch (NoSuchMethodException e) {
                Log.e("tag", "onMenuOpened", e);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }
    return super.onMenuOpened(featureId, menu);
}

Hope it helps you all. Thanks.

希望对大家有帮助。谢谢。

回答by Guihgo

use the same context of the ActionBarto create the PopupMenu

使用相同的上下文ActionBar来创建PopupMenu

actionBar.getThemedContext()

actionBar.getThemedContext()

So,

所以,

ActionBar actionBar = ((ActionBarActivity) getActivity()).getSupportActionBar();
PopupMenu popMenu = new PopupMenu(actionBar.getThemedContext(), someView);

回答by Kirill Smirnov

I am styling popup menu a little bit differently than others.

我的弹出菜单样式与其他样式略有不同。

So, I've created a style

所以,我创造了一种风格

<style name="AppTheme.PopupMenu" parent="Widget.AppCompat.PopupMenu">
    <item name="android:textColor">@color/textContrast</item>
    <item name="android:textColorSecondary">@color/text</item>
</style>

And I'm using toolbar

我正在使用工具栏

Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

And set popup style like this

并像这样设置弹出样式

toolbar.setPopupTheme(R.style.AppTheme_PopupMenu);