将String添加到textView但更改其颜色

时间:2014-12-19 07:14:18

标签: android layout textview

我在论坛上找了一个答案,我试着这样做但是由于某种原因,颜色没有改变,有什么我做错了吗?

SpannableString wordtoSpan = new SpannableString(songSelected);
        wordtoSpan.setSpan(
             new ForegroundColorSpan(getResources().getColor(R.color.red)),
             wordtoSpan.length()-1, 
             wordtoSpan.length(), 
             Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        selected_song.setText("Selected song is: "+ wordtoSpan);

1 个答案:

答案 0 :(得分:0)

像这样使用

String styledText = "This is <font color='red'>simple</font>.";
textView.setText(Html.fromHtml(styledText), TextView.BufferType.SPANNABLE);