我正在尝试更改Preference Activity中的字体,它工作得很好,所有标题和摘要字体都已更改,但ListPreference的摘要显示为默认字体
private void convertPreferenceToUseCustomFont(Preference somePreference) {
Typeface font = Typeface.createFromAsset(getActivity().getAssets(), "font.ttf");
Spannable ss;
if (somePreference.getTitle() != null) {
ss = new SpannableStringBuilder(somePreference.getTitle().toString());
ss.setSpan(new CustomTypefaceSpan("" , font), 0 , ss.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
somePreference.setTitle(ss);
}
if (somePreference.getSummary() != null) {
ss = new SpannableStringBuilder(somePreference.getSummary().toString());
ss.setSpan(new CustomTypefaceSpan("" , font), 0 , ss.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
somePreference.setSummary(ss);
}
}
这是customtypefacespan类
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);
}
}
我正在使用这种方式
ListPreference language = (ListPreference) findPreference("language");
convertPreferenceToUseCustomFont(language);
所有复选框和preferncescreen工作字体但ListPreference的摘要没有变化