原生就支持的部分html语法
官方 Supported HTML elements include: 给出了 b,i,u,但是实际上支持的还有。
根据
StringBlock.java ( frameworks\base\core\java\android\content\res)
的 get 方法源码可知,有以下类型可以支持。
"b"); ==>StyleSpan(Typeface.BOLD),
"i"); ==>StyleSpan(Typeface.ITALIC),
"u"); ==>UnderlineSpan
"tt"); ==>TypefaceSpan("monospace"),
"big"); ==>RelativeSizeSpan(1.25f),
"small"); ==>RelativeSizeSpan(0.8f),
"sup"); ==>SubscriptSpan(),//上下标
"sub"); ==>SuperscriptSpan(),
"strike"); ==>StrikethroughSpan(),//删除线
"li"); ==>new
BulletSpan(10),//用在首位,多个列表的圆点符号
"marquee"); TextUtils.TruncateAt.MARQUEE
由其applyStyles 方法可知还支持
"font;":
由其applyStyles 方法可知还支持
"font;":
";height=" ==>Height(size),
";size=" ==>AbsoluteSizeSpan(size, true),
";fgcolor=" ==>ForegroundColorSpan(c);
";fgcolor=" ==>ForegroundColorSpan(c);
";color=" ==>ForegroundColorSpan(c);
";bgcolor=" ==>BackgroundColorSpan(c);
";face="
==>TypefaceSpan(sub),
“a;”:
”;href=“ ==>URLSpan(sub),
"annotation;" ==>Annotation(key,
value),
参考:
<string name="test">bold for <b>test</b> small for <small>this is the small string</small></string>
<string name="sms_short_code_details">This <font fgcolor="#ffffb060">may cause charges</font> on your mobile account.</string>其中font的颜色既fgcolor属性要以Android的颜色格式指定,比如 #FF00FF00 ,要带上前面的透明度,不然透明度默认是0,既类似 #00FF0000 这样,会把字弄没。
标准HTML标签
方法1、使用 CDATA(推荐)
<string name="demoStr"><Data><![CDATA[ <b>ABC</b> ]]> </Data></string>
方法2、转义HTML标签
<string name="myHeadStr"><b><u>bold, underline </u></b></string>
在程序里引用:
Html.fromHtml(getResources().getString(R.string.myHeadStr));