美文网首页
Android Html设置TextView的颜色、加粗样式

Android Html设置TextView的颜色、加粗样式

作者: IT一书生 | 来源:发表于2018-03-09 16:38 被阅读55次

01、概述

在开发需求中,会遇到字符串中的某个部分变色,加粗的需求。当然可是使用SpannableString 来实现,这里记录一下通过Html方式来实现这种效果

02、代码

textView.setText(Html.fromHtml(this.getString(R.string.ceshi, "01.01",  "26")));  

textView1.setText(Html.fromHtml(this.getString(R.string.ceshi1, "01.01",  "26")));  

textView2.setText(Html.fromHtml(this.getString(R.string.ceshi2, "不加粗")));  

03、资源

不转义:生日(加粗)= %1$s 年龄(不加粗)= %2$s]]>  

—转义:生日(加粗)= %1$s 年龄(不加粗)= %2$s  

不加粗:%1$s  

04、补充:用String 格式化

 代码:

textView3.setText(String.format(getString(R.string.ceshi3), 9));  

textView4.setText(String.format(getString(R.string.ceshi4),"张三"));  

资源:

消息个数(%1$d)  

你的名字:%1$s  

05、补充:用String 格式化 Html 的转义符号

这里记录几个字符,有其他特殊需求,可以参考Html 手册 http://www.w3school.com.cn/tags/html_ref_symbols.html

代码:

textView5.setText(String.format(getString(R.string.ceshi5), 22));  

textView6.setText(String.format(getString(R.string.ceshi6)));  

textView7.setText(String.format(getString(R.string.ceshi7)));  

资源:

年龄\u2000%1$d岁  

加载中\u2026  

数字6& #8211;20范围  

相关文章

网友评论

      本文标题:Android Html设置TextView的颜色、加粗样式

      本文链接:https://www.haomeiwen.com/subject/kmfmfftx.html