@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_text_view); TextView tv = (TextView)findViewById(R.id.tv); String str="怎么改变文字呢怎么改变文字呢怎么改变文字呢怎么改变文字呢怎么改变文字呢"; SpannableStringBuilder style = new SpannableStringBuilder(str); style.setSpan(new ForegroundColorSpan(Color.RED), 0, 10, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); style.setSpan(new ForegroundColorSpan(Color.GREEN), 10, 15, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); style.setSpan(new ForegroundColorSpan(Color.BLUE), 15, 20, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); tv.setText(style); }
如下图: