Android-修改TextView中部分文字的颜色

来源:互联网 发布:爱养成吸血族女王数据 编辑:程序博客网 时间:2024/05/17 09:00

 

[java] view plaincopy
  1. textView = (TextView) findViewById(R.id.textview);  
  2. SpannableStringBuilder builder = new SpannableStringBuilder(textView.getText().toString());  
  3.   
  4. //ForegroundColorSpan 为文字前景色,BackgroundColorSpan为文字背景色  
  5. ForegroundColorSpan redSpan = new ForegroundColorSpan(Color.RED);  
  6. ForegroundColorSpan whiteSpan = new ForegroundColorSpan(Color.WHITE);  
  7. ForegroundColorSpan blueSpan = new ForegroundColorSpan(Color.BLUE);  
  8. ForegroundColorSpan greenSpan = new ForegroundColorSpan(Color.GREEN);  
  9. ForegroundColorSpan yellowSpan = new ForegroundColorSpan(Color.YELLOW);  
  10.   
  11.   
  12.   
  13. builder.setSpan(redSpan, 01, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
  14. builder.setSpan(whiteSpan, 12, Spannable.SPAN_INCLUSIVE_INCLUSIVE);  
  15. builder.setSpan(blueSpan, 23, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
  16. builder.setSpan(greenSpan, 34, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
  17. builder.setSpan(yellowSpan, 4,5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
  18.   
  19. textView.setText(builder);  
0 0
原创粉丝点击