我正在使用以下代码行来使用remoteView.setTextViewText(R.id.folder_widget_text_view, SS);
在窗口小部件上设置自定义字体,但它根本不起作用,如果我在普通TextView.setText(SS)
方法上使用相同的代码,则可以正常工作。
我的代码中有错误或错过了什么。
SpannableStringBuilder SS = new SpannableStringBuilder("hello");
SS.setSpan (new CustomTypefaceSpan(this, "Rosemary.ttf"), 0, SS.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
remoteView.setTextViewText(R.id.folder_widget_text_view, SS);
CustomTypefaceSpan Class
public class CustomTypefaceSpan extends MetricAffectingSpan
{
private static LruCache<String, Typeface> sTypefaceCache =
new LruCache<String, Typeface>(12);
private Typeface mTypeface;
public CustomTypefaceSpan(Context context, String typefaceName)
{
mTypeface = sTypefaceCache.get(typefaceName);
if (mTypeface == null)
{
mTypeface = Typeface.createFromAsset(context.getAssets(), typefaceName);
// Cache the loaded Typeface
sTypefaceCache.put(typefaceName, mTypeface);
}
}
@Override
public void updateMeasureState(TextPaint p)
{
p.setTypeface(mTypeface);
// Note: This flag is required for proper typeface rendering
p.setFlags(p.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
}
@Override
public void updateDrawState(TextPaint tp)
{
tp.setTypeface(mTypeface);
// Note: This flag is required for proper typeface rendering
tp.setFlags(tp.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
}
}