介绍
如何在Android中利用TextView对字体颜色进行更改?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
<强> Android实现不同字体颜色的TextView 强>
<强>实现代码:强>
包com.chuck.recyclerviewdemo; 进口android.content.Context; 进口android.graphics.Canvas; 进口android.text.SpannableStringBuilder; 进口android.text.Spanned; 进口android.text.style.ForegroundColorSpan; 进口android.util.AttributeSet; 进口android.widget.TextView; 进口并不知道;/* * *项目名称:躯干 *类描述: *创建人:管理员 *创建时间:2015/12/10 14:05 *修改人:管理员 *修改时间:2015/12/10 14:05 *修改备注: */公开课DifferentColorTextView延伸TextView { 公共DifferentColorTextView(上下文语境){ 超级(上下文); } 公共DifferentColorTextView(上下文语境,AttributeSet attrs) { 超级(上下文,attrs); } 公共DifferentColorTextView(上下文语境、AttributeSet attrs int defStyleAttr) { 超级(上下文、attrs defStyleAttr); } @Override 保护无效onDraw(帆布画布){ super.onDraw(画布); } 公共空间setDifferentColorText (List文本,List 颜色){ setText (calculateResidue(文本、颜色)); }/* *显示不同颜色*/私人SpannableStringBuilder calculateResidue (List 文本,List 颜色){ 如果(文本==null | |颜色==null) { 返回null; } StringBuilder某人=new StringBuilder (); for (int i=0; i< text.size();我+ +){ sb.append (text.get (i)); } SpannableStringBuilder单边带=new SpannableStringBuilder (sb.toString ()); int开始=0; for (int i=0; i< text.size();我+ +){ ForegroundColorSpan mSpan=new ForegroundColorSpan (colors.get(我)); ssb.setSpan (mSpan,开始,开始+ text.get (i) . length (), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 开始=开始+ text.get (i) . length (); } 返回单边带; } }
看完上述内容,你们掌握如何在Android中利用TextView对字体颜色进行更改的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注行业资讯频道,感谢各位的阅读!