textview可以有不同颜色的字母吗?

时间:2011-06-08 13:26:17

标签: android colors textview textcolor

我想显示'123',但是1为红色2,绿色为3,黑色为3 ...... 这是可能的,还是有其他推荐的方法在同一文本视图中显示不同的文字颜色...

3 个答案:

答案 0 :(得分:60)

是的,如果您使用SpannableString,则可以在文本的不同位置使用不同的颜色。例如:

SpannableString text = new SpannableString("Lorem ipsum dolor sit amet");  
// make "Lorem" (characters 0 to 5) red  
text.setSpan(new ForegroundColorSpan(Color.RED), 0, 5, 0);  
textView.setText(text, BufferType.SPANNABLE);

有一个更完整的例子here

Javadoc for SpannableString

答案 1 :(得分:2)

啊我发现它使用下面的代码

myTextView.setText(Html.fromHtml(html text having 1 in red 2 in green and so on));

我不懂网络,所以你最好咨询一下能为你写HTML的人:P

答案 2 :(得分:1)

您可以打印多种颜色的线条而不使用像这样的html:)

TextView TV = (TextView)findViewById(R.id.mytextview01);

  Spannable WordtoSpan = new SpannableString("Your message");        

WordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 0, WordtoSpan .length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

TV.setText(WordtoSpan);
相关问题