2012-07-19 106 views
3

我不认为我的问题需要很多解释,我只需要更改我的上下文菜单项的字体和大小。我怎么做?如何更改Android ContextMenu的字体?

这里是我的代码:

@Override 
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { 
     super.onCreateContextMenu(menu, v, menuInfo); 
     MenuInflater inflater = getMenuInflater(); 
     inflater.inflate(R.menu.listmenu, menu); 

    } 

这是我如何获取Android版的默认文本菜单。但我想定制它。

+1

你是如何创建上下文菜单的工作? – FoamyGuy 2012-07-19 13:13:11

回答

2

你可以创建自己的布局而不是R.menu.listmenu,然后设置自定义字体吗?

或者,您可以尝试拦截用户的触摸并弹出一个完全自定义的菜单,但这可能会刺激期待特定字体/菜单的用户。

2

试试这个

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
MenuInflater inflater = getMenuInflater(); 
inflater.inflate(R.menu.my_menu, menu); 
getLayoutInflater().setFactory(new Factory() { 
    @Override 
    public View onCreateView(String name, Context context, AttributeSet attrs) { 
     if (name .equalsIgnoreCase(“com.android.internal.view.menu.IconMenuItemView”)) { 
      try{ 
       LayoutInflater f = getLayoutInflater(); 
       final View view = f.createView(name, null, attrs); 
       new Handler().post(new Runnable() { 
        public void run() { 
         // set the background drawable 
         view .setBackgroundResource(R.drawable.my_ac_menu_background); 

         // set the text color 
         ((TextView) view).setTextColor(Color.WHITE); 
        } 
       }); 
       return view; 
      } catch (InflateException e) { 
       } catch (ClassNotFoundException e) {} 
     } 
     return null; 
    } 
}); 
return super.onCreateOptionsMenu(menu); 
} 
+1

那么这也适用于onCreateContextMenu? – Wise 2012-07-20 07:21:59

+0

可能是..试试吧 – 2012-07-20 09:11:21

0

测试并般的魅力:)

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.menu_feedback_filter, menu); 

    for (int i = 0; i < menu.size(); i++) { 
     MenuItem mi = menu.getItem(i); 
     //for aapplying a font to subMenu ... 
     SubMenu subMenu = mi.getSubMenu(); 
     if (subMenu != null && subMenu.size() > 0) { 
      for (int j = 0; j < subMenu.size(); j++) { 
       MenuItem subMenuItem = subMenu.getItem(j); 
       applyFontToMenuItem(subMenuItem, typeface); 
      } 
     } 
     //the method we have create in activity 
     applyFontToMenuItem(mi, typeface); 
    } 

    return super.onCreateOptionsMenu(menu); 
} 



private void applyFontToMenuItem(MenuItem mi, Typeface font) { 
    SpannableString mNewTitle = new SpannableString(mi.getTitle()); 
    mNewTitle.setSpan(new CustomTypefaceSpan("", font), 0, mNewTitle.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE); 
    mi.setTitle(mNewTitle); 
} 

定制跨度类

import android.graphics.Paint; 
import android.graphics.Typeface; 
import android.text.TextPaint; 
import android.text.style.TypefaceSpan; 

public class CustomTypefaceSpan extends TypefaceSpan { 

    private final Typeface newType; 

    public CustomTypefaceSpan(String family, Typeface type) { 
     super(family); 
     newType = type; 
    } 

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

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

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