同一TextView中字符串的字体大小和颜色不同

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了同一TextView中字符串的字体大小和颜色不同相关的知识,希望对你有一定的参考价值。

是否可以在同一文本视图中更改字符串的字体大小和颜色。

例如 :

 _______________
|_______________| --> this is my text view 

我想做的事

________________
|ABC (red color) |
|abc(whitecolor) |
|________________|

字符串应该看起来像这样的“ABC nabc”

答案

使用SpannableStringBuilder

SpannableStringBuilder builder = new SpannableStringBuilder();

SpannableString str1= new SpannableString("Text1");
str1.setSpan(new ForegroundColorSpan(Color.RED), 0, str1.length(), 0);
builder.append(str1);

SpannableString str2= new SpannableString(appMode.toString());
str2.setSpan(new ForegroundColorSpan(Color.WHITE), 0, str2.length(), 0);
builder.append(str2);

TextView tv = (TextView) view.findViewById(android.R.id.text1);
tv.setText( builder, TextView.BufferType.SPANNABLE);

以上是关于同一TextView中字符串的字体大小和颜色不同的主要内容,如果未能解决你的问题,请参考以下文章