本文章转自:http://blog.youkuaiyun.com/centralperk/article/details/8674599

- 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);
本文介绍如何使用 Android 中 TextView 控件的 SpannableStringBuilder 来设置不同部分文字的颜色。通过 ForegroundColorSpan 可以实现对 TextView 中特定位置的文字进行前景色设置。
682

被折叠的 条评论
为什么被折叠?



