Android TextView里显示两种颜色

今天介绍一个小技巧,在Android的TextView里设置两种颜色,直接上代码:

TextView TV = (TextView)findViewById(R.id.mytextview01);
Spannable WordToSpan = new SpannableString("Yourmessage");       

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

TV.setText(WordToSpan);
Spannable WordToSpan1 = new SpannableString("Your newmessage");       

WordToSpan1.setSpan(new ForegroundColorSpan(Color.RED), 0,WordToSpan1.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
TV.append(WordToSpan1);

你可能感兴趣的:(Android)