记得之前用TextView设置Html文本时发现字体的大小改变不了,后来用了SpannableString解决了就不用Html了,也没查是什么原因。
这两天又用上了Html在TextView上设置富文本,如下:
我想把100元字体变大,但是并不生效。发现<font>标签不好使我就研究了一下,原来Android中提供的Html类中对标签的支持比较简单,而且<font>标签只支持color和face两个属性,源码如下:
public class Html {/*********************************************省略*///解析开始标签 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { handleStartTag(localName, attributes); }//解析结束标签 public void endElement(String uri, String localName, String qName) throws SAXException { handleEndTag(localName); } private void handleStartTag(String tag, Attributes attributes) { if (tag.equalsIgnoreCase("br")) { //省略其他标签,大概支持24种 }else if (tag.equalsIgnoreCase("font")) { startFont(mSpannableStringBuilder, attributes); } } private void handleEndTag(String tag) { if (tag.equalsIgnoreCase("br")) { handleBr(mSpannableStringBuilder); }else if (tag.equalsIgnoreCase("font")) { endFont(mSpannableStringBuilder); }else if(){ //省略 } } private void startFont(Editable text, Attributes attributes) { String color = attributes.getValue("", "color"); String face = attributes.getValue("", "face"); if (!TextUtils.isEmpty(color)) { int c = getHtmlColor(color); if (c != -1) { start(text, new Foreground(c | 0xFF000000)); } } if (!TextUtils.isEmpty(face)) { start(text, new Font(face)); } } private static void endFont(Editable text) { Font font = getLast(text, Font.class); if (font != null) { setSpanFromMark(text, font, new TypefaceSpan(font.mFace)); } Foreground foreground = getLast(text, Foreground.class); if (foreground != null) { setSpanFromMark(text, foreground, new ForegroundColorSpan(foreground.mForegroundColor)); } } private static class Font { public String mFace; public Font(St3分快三计划 private void startFont(Editable text, Attributes attributes) { String color = attributes.getValue("", "color"); String face = attributes.getValue("", "face"); if (!TextUtils.isEmpty(color)) { int c = getHtmlColor(color); if (c != -1) { start(text, new Foreground(c | 0xFF000000)); } } if (!TextUtils.isEmpty(face)) { start(text, new Font(face)); } } private static void endFont(Editable text) { Font font = getLast(text, Font.class); if (font != null) { setSpanFromMark(text, font, new TypefaceSpan(font.mFace)); } Foreground foreground = getLast(text, Foreground.class); if (foreground != null) { setSpanFromMark(text, foreground, new ForegroundColorSpan(foreground.mForegroundColor)); } } private static class Font { public String mFace; public Font(String face) { mFace = face; } }/*********************************************省略*/}把Html类拷贝出来,给它加一个size的属性行不行呢?我试过了,不好使又麻烦。
后来我参考其他文章,发现最简单的方法还是使用Html的自定义标签。即Html.fromHtml(String source, ImageGetter imageGetter, TagHandler tagHandler);方法。
自定义一个<size>标签,可以自己随便起,代码如下:
然后我把html文本改了一下,加上了<size>标签,然后效果就出来了。
String html= "<html><body style='font-size:15px;color:#57AFFC'><p>18888元大礼包免费送给你<br />等等,还没完,还有<br /><font size='20px'><size>100元</size></font>返现,用不用随你</p></body></html>";textview.setText(Html.fromHtml(html,null,new SizeLabel(30)));由于这段文字是请求接口返回的,所以我不能确定要变大字体的位置,要不参考SpannableString很容易就解决了,我便让后台小哥帮我加了这么个标签,对IOS不影响(不要自定义系统支持的标签避免冲突)。
OK,搞定了这么个烦人的小问题。
啦啦啦
啦啦啦
你以为结束了?还没呢╮(╯▽╰)╭
TextView设置Gravity为center后对Html文本不生效,只能左右居中,不能上下居中,WebView也是如此。小问题可真多啊,没办法,只能按照效果图来了,给TextView设置了padding,看起来才像回事。这个问题等有空再来研究分析一下,谢谢观看。
参考连接
http://blog.csdn.net/chaoyue0071/article/details/47614971?locationNum=3&fps=1
http://blog.csdn.net/u010418593/article/details/9322197