android 更改TextView中任意位置字体大小和颜色 android 更改TextView中任意位置字体大小和颜色的方法

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

android 更改TextView中任意位置字体大小和颜色 android 更改TextView中任意位置字体大小和颜色的方法

keyboy_rl   2021-03-28 我要评论
想了解android 更改TextView中任意位置字体大小和颜色的方法的相关内容吗,keyboy_rl在本文为您仔细讲解android 更改TextView中任意位置字体大小和颜色的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:android,TextView,字体,大小,颜色,下面大家一起来学习吧。

这里介绍两种方法,一种是Spannable,一种是Html.fromHtml(通过html标签来改变),实际中看您使用哪种方便选择使用即可

1.Html.fromHtml的使用

TextView textView = (TextView) findViewById(R.id.text);
String textSource = "修改TextView中部分文字的<font color='#ff0000'><big>大</big><small>小</small></font>和<font color='#00ff00'>颜色</font>,展示多彩效果!";
textView.setText(Html.fromHtml(textSource));

上面是没有加html标签,下面是加了html标签的效果:

2.使用Spannable来实现

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

使用Spannable效果如下图:

是不是很简单,但是效果强大,赶紧来实现吧!!!

以上这篇android 更改TextView中任意位置字体大小和颜色的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们