PopupMenu - Customize Text (Size/Color) in Menu Resource Items

你。 提交于 2019-12-13 06:50:54

问题


I have created a PopupMenu that is inflated OnClick of a LinearLayout. I want the Menu Items to have a specific textColor and textSize; however, Menu Items seem to have a very restricted set of XML attributes and I can't find a way to reference a style. Any ideas?

PopupMenu:

customerMenu = (LinearLayout) view.findViewById(R.id.customerMenu);
customerMenu.setOnClickListener(new OnClickListener() {
       @Override
       public void onClick(View v) {
        // TODO Auto-generated method stub
        PopupMenu popup = new PopupMenu(getActivity(), customerMenu);
        popup.getMenuInflater().inflate(R.menu.addcustomermenu, popup.getMenu());
        popup.show();
       }
});

Menu Resource:

res/menu/addcustomermenu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/customerButtonOne"
        android:title="Create New Customer" >
    </item>
    <item
        android:id="@+id/customerButtonTwo"
        android:title="Search Existing Customer">
    </item>

</menu>

回答1:


I will give you a more detailed answer to this question.

The PopupMenu is created like this,

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.

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

Styling of the PopupMenu items

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>
            <item name="android:textSize">@dimen/text_size</item>
</style>

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

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

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

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

Though the styling options is quite limited but still you can customize some basic attributes obviously.




回答2:


as far as I know.. the text of menu item will not be changed..

so.. most of the APP use the basic text of menu item..

this is only I think..



来源:https://stackoverflow.com/questions/19170962/popupmenu-customize-text-size-color-in-menu-resource-items

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!