Android TextView里显示两种颜色

时间:2023-11-25 14:47:38

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

 TextView TV = (TextView)findViewById(R.id.mytextview01);
Spannable WordToSpan = new SpannableString("Your message"); WordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 0, WordToSpan.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); TV.setText(WordToSpan);
Spannable WordToSpan1 = new SpannableString("Your new message"); WordToSpan1.setSpan(new ForegroundColorSpan(Color.RED), 0, WordToSpan1.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
TV.append(WordToSpan1);