我的应用程序支持少量语言,即使该设备未提供语言支持。为此,我正在下载TTF文件并动态设置TTF文件到所有视图。除了选项菜单以外,它在任代码添加在下面。
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
Typeface typeface = Utils.getTypefaceForAppLanguage(getContext());
String title = Utils.getLanguageValue(getString(R.string.action_apply));
inflater.inflate(R.menu.menu_apply, menu);
menu.findItem(R.id.action_apply).setTitle(typeface != null ? CommonUtils.getStringWithTypeface(typeface, title) : title);
super.onCreateOptionsMenu(menu, inflater);
}
public static SpannableString getStringWithTypeface(Typeface typeface, String str) {
SpannableString spannableString = new SpannableString(str);
if (!TextUtils.isEmpty(str)) {
spannableString.setSpan(new CustomTypefaceSpan("", typeface), 0, spannableString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
return spannableString;
}
public class CustomTypefaceSpan extends TypefaceSpan{
private Typeface newType;
public CustomTypefaceSpan(String family) {
super(family);
}
public CustomTypefaceSpan(Parcel src) {
super(src);
}
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);
}
}