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

时间:2014-07-09 11:52:10

标签: android colors popupmenu

我需要更改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>

哪里出错?

5 个答案:

答案 0 :(得分:39)

在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>

并添加AppTheme

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

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>

答案 1 :(得分:22)

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

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

你必须使用:

TextAppearance.AppCompat.Light.Widget.PopupMenu。

答案 2 :(得分:18)

在styles.xml中:

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

在java中:

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

答案 3 :(得分:2)

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

PopupMenu是这样创建的,

PopupMenu popupMenu = new PopupMenu(context, anchorView);

现在,“背景”在造型中起着非常重要的作用。 PopupMenu的样式取决于您传递的上下文的样式。所以要非常小心。我浪费了差不多一半时间来搞清楚这一点。

如果你在一个片段中,只需传递“getActivity()”就可以了。

PopupMenu项目的样式

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

<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>

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

<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>

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

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

高级样式

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

要实现这一点,只需在您的活动中添加以下代码,就可以了,

  @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);
    }

希望它有所帮助。

答案 4 :(得分:1)

//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>