TextView textview = (TextView) findViewById(R.id.color_text);
//声明一个字符串变量
String str="君不见黄河之水天上来";
SpannableStringBuilder style=new SpannableStringBuilder(str);
//从第2到第4个字符 颜色为红色
style.setSpan(new ForegroundColorSpan(Color.RED), 1, 4, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
//从第6到第7个字符 颜色为绿色
style.setSpan(new ForegroundColorSpan(Color.BLUE), 5, 7, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
textview.setText(style);