android弹出菜单文本颜色(AppCompat)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24652964/
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 08:33:21  来源:igfitidea点击:

android popup menu text color (AppCompat)

androidcolorspopupmenu

提问by francesco.venica

I need to change text color of a popuo menu but I don't find any way for do this, I can change background of popmenu but not the text, I edit the style.xml in this way:

我需要更改 popuo 菜单的文本颜色,但我找不到任何方法来执行此操作,我可以更改 popmenu 的背景但不能更改文本,我以这种方式编辑 style.xml:

<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
    <!-- API 14 theme customizations can go here. -->

    <item name="popupMenuStyle">@style/MyPopupMenu</item>
    <item name="android:textAppearanceLargePopupMenu">@style/myPopupMenuTextAppearanceLarge</item>
    <item name="android:textAppearanceSmallPopupMenu">@style/myPopupMenuTextAppearanceSmall</item>
</style>

<style name="MyPopupMenu" parent="@style/Widget.AppCompat.PopupMenu">
    <item name="android:popupBackground">#0F213F</item>
</style>

<style name="myPopupMenuTextAppearanceSmall" parent="@style/TextAppearance.AppCompat.Base.Widget.PopupMenu.Small">
    <item name="android:textColor">#ffffff</item>
</style>

<style name="myPopupMenuTextAppearanceLarge" parent="@style/TextAppearance.AppCompat.Base.Widget.PopupMenu.Large">
    <item name="android:textColor">#ffffff</item>
</style>

where is the mistake?

错误在哪里?

采纳答案by Alfaplus

<item name="textAppearanceLargePopupMenu">@style/TextAppearance.AppCompat.Light.Widget.PopupMenu.Large</item>
<item name="textAppearanceSmallPopupMenu">@style/TextAppearance.AppCompat.Light.Widget.PopupMenu.Small</item>

I think that you are using TextAppearance.AppCompat.Base.Widget.PopupMenu. Here is the error, you are using another parent that doesn′t response the current style.

我认为您正在使用 TextAppearance.AppCompat.Base.Widget.PopupMenu。这是错误,您正在使用另一个不响应当前样式的父项。

You have to use:

你必须使用:

TextAppearance.AppCompat.Light.Widget.PopupMenu.

TextAppearance.AppCompat.Light.Widget.PopupMenu。

回答by Juan Labrador

In styles.xml

在styles.xml中

<style name="itemTextStyle.AppTheme" parent="@android:style/TextAppearance.Widget.IconMenu.Item">
        <item name="android:textColor">@drawable/color_item_popup</item>
        <item name="android:textSize">@dimen/text_content</item>
    </style>

and add in AppTheme

并添加 AppTheme

<item name="android:itemTextAppearance">@style/itemTextStyle.AppTheme</item>

color_item_popup.xml

color_item_popup.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:color="@color/primary_text"/>
    <item android:state_focused="true" android:color="@color/primary_text"/>
    <item android:color="@color/secondary_text"/>
</selector>

回答by Junior Galdino

In styles.xml:

在styles.xml中:

<style name="PopupMenu" parent="Widget.AppCompat.PopupMenu.Overflow">
    <item name="android:popupBackground">@color/white</item>
</style>

In java:

在Java中:

Context wrapper = new ContextThemeWrapper(getContext(), R.style.PopupMenu); final PopupMenu popupMenu = new PopupMenu(wrapper, view);

Context wrapper = new ContextThemeWrapper(getContext(), R.style.PopupMenu); final PopupMenu popupMenu = new PopupMenu(wrapper, view);

回答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.

希望能帮助到你。

回答by Tousif Akram

//In Main Style
<item name="popupMenuStyle">@style/popupMenuStyle</item>
    <item name="android:textAppearanceSmallPopupMenu">@style/myPopupMenuTextAppearanceSmall</item>
    <item name="android:textAppearanceLargePopupMenu">@style/myPopupMenuTextAppearanceLarge</item>


  //In Define part

   <style name="popupMenuStyle" parent="Widget.AppCompat.PopupMenu">
    <item name="android:popupBackground">@drawable/popup_bg</item>
    <item name="android:textColor">#ffffff</item>
</style>
<style name="myPopupMenuTextAppearanceSmall" parent="@style/TextAppearance.AppCompat.Widget.PopupMenu.Small">
    <item name="android:textColor">#ffffff</item>
</style>

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

  //you may use this background 
 //popup_bg.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
    android:angle="135"
    android:centerColor="#c8232323"
    android:endColor="#c80d0d0d"
    android:startColor="#c8434242"
    android:type="linear" />
</shape>