2014-11-16 2 views
7

내가 의 PopupMenu 항목의 기본 글꼴을 변경하고 그들을 위해 내 사용자 지정 글꼴에서 사용할 글꼴.변경하는 방법의 PopupMenu 항목은

PopupMenu pm = new PopupMenu(this, v); 
getMenuInflater().inflate(R.menu.main, pm.getMenu()); 
pm.show(); 

그리고 메뉴 항목 :

이 내가 PopupMenu를을 만드는 데 사용되는 코드입니다

<menu xmlns:android="http://schemas.android.com/apk/res/android" > 
    <item 
     android:id="@+id/Setting" 
     android:title="Setting"/> 
    <item 
     android:id="@+id/About" 
     android:title="About"/> 
    <item 
     android:id="@+id/Help" 
     android:title="Help"/> 
</menu> 

당신이 나와 함께 당신의 제안을 공유하는 경우 내가 너무 감사 할 것입니다 : -)

감사합니다.

답변

0

나는 그것이 불가능하다고 생각한다. 사실 popupWindow를 사용하여 원하는대로 메뉴를 사용자 정의 할 수 있습니다.

2

리플렉션을 사용할 수 있습니다. 팝업 메뉴 항목을 사용자 정의 할 때 사용할 수 있습니다. 안드로이드 지원 메뉴 항목의 리소스 레이아웃은 android.support.v7.internal.view.menu.MenuPopupHelper에 정의되어 있으며 필드 이름은 static final로 선언 된 "ITEM_LAYOUT"입니다. 그 값은 "R.layout.abc_popup_menu_item_layout"과 같습니다. Grepcode에서 레이아웃 파일을 찾고 그것을 프로젝트 레이아웃 디렉토리에 복사합니다. 나는 popup_menu_item_layout.xml이라는 이름을 붙였다. 내 팝업 메뉴 항목의 레이아웃은

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

<!-- Icon will be inserted here. --> 

<!-- The title and summary have some gap between them, and this 'group' should be centered vertically. --> 
<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> 

<!-- Checkbox, and/or radio button will be inserted here. --> 

그런 다음 사용자 정의 클래스 PopupMenuItemView 만들고 여기에 온다 :

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

마지막으로 반사하여 메뉴 항목 레이아웃 리소스 ID를 교체하십시오;

팝업 메뉴 Mehthod :

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

이 같은 문제에 대한 내 솔루션을 확인하십시오 : 일부는 주요 활동에서 onCreate 방법 또는 응용 프로그램에서 onCreate 방법처럼 어디에서

private void showEditPopupWindow(Context mContext) { 
     PopupMenu popupMenu = new PopupMenu(mContext, view); 
     popupMenu.getMenuInflater().inflate(R.menu.YOUR_MENU, popupMenu.getMenu()); 
     popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { 
      @Override 
      public boolean onMenuItemClick(MenuItem item) { 
       if (item.getItemId() == R.id.delete) { 
        // Do your stuffs; 
       } else { 
        // Do your stuffs 
       } 
       return true; 
      } 
     }); 

     Menu menu = popupMenu.getMenu(); 
     for (int i = 0; i < menu.size(); i++) { 
      MenuItem mi = menu.getItem(i); 
      applyFontToMenuItem(mi); 
     } 

    } 

이에 글꼴 적용 글꼴의 색상을 변경할 수도 있습니다.

private void applyFontToMenuItem(MenuItem mi) { 
     Typeface font = Typeface.createFromAsset(mContext.getAssets(), "fonts/YOUR_FONT.ttf"); 
     SpannableString mNewTitle = new SpannableString(mi.getTitle()); 
     mNewTitle.setSpan(new CustomTypeFaceSpan("", font,Color.WHITE), 0, mNewTitle.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE); 
     mi.setTitle(mNewTitle); 
    } 

CustomTypeFaceSpan 클래스 :

public class CustomTypeFaceSpan extends TypefaceSpan { 

private final Typeface newType; 
private final int mColor; 

public CustomTypeFaceSpan(String family, Typeface type, @ColorInt int color) { 

    super(family); 
    newType = type; 
    mColor = color; 
} 

@Override 
public void updateDrawState(TextPaint ds) { 
    ds.setColor(mColor); 
    applyCustomTypeFace(ds, newType); 
} 

@Override 
public void updateMeasureState(TextPaint paint) { 
    applyCustomTypeFace(paint, newType); 
} 

@Override 
public int getSpanTypeId() { 
    return super.getSpanTypeId(); 
} 

@ColorInt 
public int getForegroundColor() { 
    return mColor; 
} 




private static void applyCustomTypeFace(Paint paint, Typeface tf) { 
    int oldStyle; 
    Typeface old = paint.getTypeface(); 
    if (old == null) { 
     oldStyle = 0; 
    } else { 
     oldStyle = old.getStyle(); 
    } 

    int fake = oldStyle & ~tf.getStyle(); 
    if ((fake & Typeface.BOLD) != 0) { 
     paint.setFakeBoldText(true); 
    } 

    if ((fake & Typeface.ITALIC) != 0) { 
     paint.setTextSkewX(-0.25f); 
    } 

    paint.setTypeface(tf); 
} 

}

관련 문제