您的位置:首页 > 其它

textview中的文字不同颜色

2016-05-19 10:35 302 查看
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); 

注意:每一个span不能重复使用,如果需要使用两次,请重新newspan!!!!!!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: