在strings.xml中定义html标签
2013-08-29 18:47
399 查看
在项目的开发过程中,需要用到把html内容放到strings.xml文件中,然后再读取到TextView中.原本以为像普通文本一样直接SetText就行了,结果行不通,大大超出我的预料.经过网上搜索,找到一些方法,自己经过整理,在此记录下来,另附上自己的测试工程.
先贴上strings.xml文件中重点内容:
方法一:普通html string 加上Context的getText
这方法能处理html中和xml共有的标签,但无法正确解析像"<br />"这种xml所没有的标签.容易造成某些标签被忽略,实际上是用xml标签实现html标签的效果,且只能用Context的getText获取带有格式的html文本,如果用Context的getString获取,html文本的格式标签会被自动过滤掉.此方法不需要用到Html类.部分代码如下:
方法二:特殊处理的html string 加上Context的getString(或者getText).
这个方法需要对strings.xml文件中对应的string进行处理,在html内容最前面加上"<![CDATA[",在html内容末尾加上"]]",在使用的时候直接通过Context的getString(或者getText)方法获取,然后使用Html的fromHtml方法得到html内容对应的Spanned,最后调用TextView的SetText即可.部分代码如下:
对应测试工程下载地址:http://download.csdn.net/detail/banket004/6033697
效果图如下:
参考:http://stackoverflow.com/questions/3235131/set-textview-text-from-html-formatted-string-resource-in-xml/18199543#18199543
先贴上strings.xml文件中重点内容:
<string name="msg1"> <b>Hello world!</b><br/> <a href="http://blog.csdn.net/Banket004">link</a> </string> <string name="msg2"> <![CDATA[ <b>Hello world!</b><br/> <a href="http://blog.csdn.net/Banket004">link</a> ]]> </string>
方法一:普通html string 加上Context的getText
这方法能处理html中和xml共有的标签,但无法正确解析像"<br />"这种xml所没有的标签.容易造成某些标签被忽略,实际上是用xml标签实现html标签的效果,且只能用Context的getText获取带有格式的html文本,如果用Context的getString获取,html文本的格式标签会被自动过滤掉.此方法不需要用到Html类.部分代码如下:
TextView view1 = (TextView)findViewById(R.id.textView1); TextView view2 = (TextView)findViewById(R.id.textView2); TextView view3 = (TextView)findViewById(R.id.textView3); TextView view4 = (TextView)findViewById(R.id.textView4); TextView view5 = (TextView)findViewById(R.id.textView5); TextView view6 = (TextView)findViewById(R.id.textView6); TextView view7 = (TextView)findViewById(R.id.textView7); TextView view8 = (TextView)findViewById(R.id.textView8); view1.setText(getString(R.string.msg1)); view2.setText(getText(R.string.msg1)); view3.setText(Html.fromHtml(getString(R.string.msg1))); view4.setText(Html.fromHtml(getText(R.string.msg1).toString()));
方法二:特殊处理的html string 加上Context的getString(或者getText).
这个方法需要对strings.xml文件中对应的string进行处理,在html内容最前面加上"<![CDATA[",在html内容末尾加上"]]",在使用的时候直接通过Context的getString(或者getText)方法获取,然后使用Html的fromHtml方法得到html内容对应的Spanned,最后调用TextView的SetText即可.部分代码如下:
view5.setText(getString(R.string.msg2)); view6.setText(getText(R.string.msg2)); view7.setText(Html.fromHtml(getString(R.string.msg2))); view8.setText(Html.fromHtml(getText(R.string.msg2).toString()));
对应测试工程下载地址:http://download.csdn.net/detail/banket004/6033697
效果图如下:
参考:http://stackoverflow.com/questions/3235131/set-textview-text-from-html-formatted-string-resource-in-xml/18199543#18199543
相关文章推荐
- 在strings.xml中定义html标签
- 在strings.xml中定义html标签
- Android strings.xml文件定义字符串中的标签
- Android积累之《Android strings.xml文件定义字符串中的标签》
- Android积累之《Android strings.xml文件定义字符串中的标签》
- HTML“pre”标签 :用pre元素可定义预先格式文字。所有在pre元素内文字的空白间隔以及换行都会被保留。
- HTML <!DOCTYPE> 标签定义和用法
- xml去掉schema中没定义的标签
- html基础-图片标签、表格的属性、链接的属性及链接的分类、name定义锚点的名称、网页的自动刷新、基本字符
- 把预定义的字符串转化为html标签
- Android string.xml中使用html标签
- css 定义 html 页标签 没有的属性 超级好用 看了你就会明白了
- jsp 将html字符串输出html标签(escapeXml)
- PHP strip_tags()去除HTML、XML以及PHP的标签介绍
- HTML用来显示计算机代码的标签 pre 预定义格式标签
- 自己定义html中a标签的title提示tooltip
- XML中含有html标签的问题
- 一个在XML中写入HTML格式标签的小经验
- html基础--标签,属性,文本格式化标签,计算机输出标签,引用和术语定义
- HTML的一些标签定义