添加一个字符串到textView,但改变它的颜色
问题描述:
我在论坛寻找答案,我试着这样做,但由于某种原因,颜色没有改变,有没有什么我做错了?添加一个字符串到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);
答
使用这样
String styledText = "This is <font color='red'>simple</font>.";
textView.setText(Html.fromHtml(styledText), TextView.BufferType.SPANNABLE);