是否有可能有多种印刷字体在同一个TextView的?有可能、有多种、字体、在同一个

2023-09-04 09:15:50 作者:此情可待成追忆

我知道如何改变文本的属性和颜色的的TextView 在Android的,但有可能有多个字样在同一个的TextView ?例如,我可以在同一时间在同一个的TextView 同时使用德罗伊德桑和Droid的单?如果是这样,怎么样?

I know how to change text attributes and color for a TextView in Android, but is it possible to have multiple Typefaces in the same TextView? For example, can I use both Droid Sans and Droid Mono in the same TextViewat the same time? If so, how?

推荐答案

其实它看起来像你可以用Spannable的帮助下做到这一点:

Actually it looks like you can do it with the help of a Spannable:

String text = "This is an example";

Spannable s = new SpannableString(text + " text");
s.setSpan(new TypefaceSpan("monospace"), 0, text.length(),
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
s.setSpan(new TypefaceSpan("serif"), text.length(), s.length(),
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

((TextView)findViewById(R.id.my_text_view)).setText(s);
 
精彩推荐
图片推荐