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

 

 
textView = (TextView) findViewById(R.id.textview);  
SpannableStringBuilder builder = new SpannableStringBuilder(textView.getText().toString());  
  
//ForegroundColorSpan 为文字前景色,BackgroundColorSpan为文字背景色  
ForegroundColorSpan redSpan = new ForegroundColorSpan(Color.RED);  
ForegroundColorSpan whiteSpan = new ForegroundColorSpan(Color.WHITE);  
ForegroundColorSpan blueSpan = new ForegroundColorSpan(Color.BLUE);  
ForegroundColorSpan greenSpan = new ForegroundColorSpan(Color.GREEN);  
ForegroundColorSpan yellowSpan = new ForegroundColorSpan(Color.YELLOW);  
  
  
  
builder.setSpan(redSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
builder.setSpan(whiteSpan, 1, 2, Spannable.SPAN_INCLUSIVE_INCLUSIVE);  
builder.setSpan(blueSpan, 2, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
builder.setSpan(greenSpan, 3, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
builder.setSpan(yellowSpan, 4,5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
  
textView.setText(builder);  

 

 

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。