4

我正在使用以下代码行在小部件上设置自定义字体,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 类

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

0 回答 0