将自定义字体应用于菜单
public static void applyFontToMenu(Menu m, Context mContext){
for(int i=0;i<m.size();i++) {
applyFontToMenuItem(m.getItem(i),mContext);
}
}
public static void applyFontToMenuItem(MenuItem mi, Context mContext) {
if(mi.hasSubMenu())
for(int i=0;i<mi.getSubMenu().size();i++) {
applyFontToMenuItem(mi.getSubMenu().getItem(i),mContext);
}
Typeface font = Typeface.createFromAsset(mContext.getAssets(), "fonts/yourCustomFont.ttf");
SpannableString mNewTitle = new SpannableString(mi.getTitle());
mNewTitle.setSpan(new CustomTypefaceSpan("", font, mContext), 0, mNewTitle.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
mi.setTitle(mNewTitle);
}
然后在活动中:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
applyFontToMenu(menu,this);
return true;
}