【Android 】TextView 局部文字变色

TextView 对于富文本效果的实现支持不支持呢?比如“局部文字颜色的变动”,“局部字体的变动”

一、需求效果

这里写图片描述

二、解决方案

针对这类问题,Android提供了 SpannableStringBuilder,方便我们自定义富文本的实现。

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);

除了上述代码中使用的 ForegroundColorSpan 和 BackgroundColorSpan之外,还有以下这些Span可以使用:

AbsoluteSizeSpan(int size) —— 设置字体大小,参数是绝对数值,相当于Word中的字体大小

RelativeSizeSpan(float proportion) —— 设置字体大小,参数是相对于默认字体大小的倍数,比如默认字体大小是x, 那么设置后的字体大小就是x*proportion,这个用起来比较灵活,proportion>1就是放大(zoom in), proportion<1就是缩小(zoom out)

ScaleXSpan(float proportion) —— 缩放字体,与上面的类似,默认为1,设置后就是原来的乘以proportion,大于1时放大(zoon in),小于时缩小(zoom out)

BackgroundColorSpan(int color) —— 背景着色,参数是颜色数值,可以直接使用android.graphics.Color里面定义的常量,或是用Color.rgb(int, int, int)

ForegroundColorSpan(int color) —— 前景着色,也就是字的着色,参数与背景着色一致

TypefaceSpan(String family) —— 字体,参数是字体的名字比如“sans”, “sans-serif”等

StyleSpan(Typeface style) —— 字体风格,比如粗体,斜体,参数是android.graphics.Typeface里面定义的常量,如Typeface.BOLD,Typeface.ITALIC等等。StrikethroughSpan—-如果设置了此风格,会有一条线从中间穿过所有的字,就像被划掉一样

三、动手试试

比如实现下图中TextView的样式

这里写图片描述

仿照上面的写法,代码很少就出来啦:

TextView tv = (TextView)view.findViewById(R.id.toast_text);

String str1 = "提交成功!\n积分";
String str2 = "+" + score1;
String str3 = "!审核通过后再";
String str4 = "+" + score2;

SpannableStringBuilder builder = new SpannableStringBuilder(str1 + str2 + str3 + str4 + "!");
builder.setSpan(new ForegroundColorSpan(Color.parseColor("#ffffa200")),
        str1.length(), (str1 + str2).length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
builder.setSpan(new ForegroundColorSpan(Color.parseColor("#ffffa200")),
        (str1 + str2 + str3).length(), (str1 + str2 + str3 + str4).length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);

tv.setText(builder);

原文地址:

http://blog.csdn.net/u010983881/article/details/52383539?locationNum=2&fps=1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值