you can use reflection. It can be used for any setting of popup menu items. The layout of the resources for a menu item in android support is defined in the android.support.v7.internal.view.menu.MenuPopupHelper file, and this is the name of the field "ITEM_LAYOUT" declared as a static ending; This value is "R.layout.abc_popup_menu_item_layout" I found the layout file in Grepcode and copied it into my project layout directory. I called it popup_menu_item_layout.xml. The layout of the pop-up menu item is displayed here.
<?xml version="1.0" encoding="utf-8"?> <mypackage.PopupMenuItemView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="?attr/dropdownListPreferredItemHeight" android:minWidth="196dip" android:paddingRight="16dip"> <RelativeLayout android:layout_width="0dip" android:layout_weight="1" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="16dip" android:duplicateParentState="true"> <TextView android:id="@+id/title" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:textAppearance="?attr/textAppearanceLargePopupMenu" android:singleLine="true" android:duplicateParentState="true" android:ellipsize="marquee" android:fadingEdge="horizontal"/> <TextView android:id="@+id/shortcut" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/title" android:layout_alignParentLeft="true" android:textAppearance="?attr/textAppearanceSmallPopupMenu" android:singleLine="true" android:duplicateParentState="true"/> </RelativeLayout>
Then create your own PopupMenuItemView class:
public class PopupMenuItemView extends android.support.v7.internal.view.menu.ListMenuItemView { public PopupMenuItemView(Context context, AttributeSet attrs) { super(context, attrs); } public PopupMenuItemView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override protected void onFinishInflate() { super.onFinishInflate(); applyTypefaceToAll(this, your_typeface); TypefaceUtils.applyTextSizeToAll(this, your_textsize); } public static void applyTypefaceToAll(View view, Typeface typeface) { if (view instanceof ViewGroup) { ViewGroup viewGroup = (ViewGroup) view; for (int childIndex = 0; childIndex < viewGroup.getChildCount(); childIndex++) applyTypefaceToAll(viewGroup.getChildAt(childIndex), typeface); } else if (view instanceof TextView) { TextView textView = (TextView) view; textView.setTypeface(typeface); textView.setPaintFlags(textView.getPaintFlags() | Paint.SUBPIXEL_TEXT_FLAG | Paint.DEV_KERN_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG); } } public static void applyTextSizeToAll(View view, float size) { if (view instanceof ViewGroup) { ViewGroup viewGroup = (ViewGroup) view; for (int childIndex = 0; childIndex < viewGroup.getChildCount(); childIndex++) applyTextSizeToAll(viewGroup.getChildAt(childIndex), size); } else if (view instanceof TextView) { TextView textView = (TextView) view; textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, size); textView.setPaintFlags(textView.getPaintFlags() | Paint.SUBPIXEL_TEXT_FLAG | Paint.DEV_KERN_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG); } } }
Finally, replace the layout resource identifier for the menu items by reflection; some where, as in your main action onCreate method or in your application onCreate method:
try { setFinalStatic(MenuPopupHelper.class.getDeclaredField("ITEM_LAYOUT"), R.layout.popup_menu_item_layout); } catch (Exception e) { e.printStackTrace(); } public static void setFinalStatic(Field field, Object newValue) throws Exception { field.setAccessible(true); try { Field modifiersField = Field.class.getDeclaredField("modifiers"); modifiersField.setAccessible(true); modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL); }catch (Exception e) { e.printStackTrace(); } field.set(null, newValue); }
Abolhassan abdolalizade
source share