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!!!!!!!
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!!!!!!!
相关文章推荐
- 为什么写博客?不是为了装逼,只是希望自己进步
- javaScript判断数据类型
- Golang 单元测试和性能测试
- 用任意多个个2*1的小矩形,覆盖2*n的大矩形,请问有多少种覆盖的方法?
- ab 模拟测试秒杀
- c++第六次上机作业
- 关于android中listview的adapter如何通用的一些看法
- 组合框,列表框
- 在项目中使用MPAndroidChart遇到的问题
- JavaEE Filter敏感词过滤的方法实例详解
- Java成长笔记——有关“包”
- ural1057(数位dp+组合数)
- 数独Sudoku
- UTC时间与北京时间
- IOS 通过苹果开放API检测更新
- jQuery
- 以Attention Model为例谈谈两种研究创新模式
- Android as Bluetooth Low Energy Peripheral (GATT server).
- javabean实体类对象转为Map类型对象的方法(转发)
- android studio 添加外部库的问题