为什么不能当我读到从资源字符串的字体颜色显示?当我、字符串、读到、字体

由网友(我们都输给了一句我以为)分享简介:我希望成立一个TextView的用两种不同颜色的文字,我的阅读文档 Single TextView中有两种不同颜色的文字 code一个效果很好!从资源文件 code B GET字符串,但无法显示字体的颜色,为什么? code A 的TextView我=(的TextView)findViewById(R.id.t...

我希望成立一个TextView的用两种不同颜色的文字,我的阅读文档

Single TextView中有两种不同颜色的文字

code一个效果很好!

从资源文件

code B GET字符串,但无法显示字体的颜色,为什么?

code A

 的TextView我=(的TextView)findViewById(R.id.tVTitleOld); 字符串文本=<字体颜色=#cc0029'>你好< / FONT>的<字体颜色=#FFCC00'>世界< / FONT>中; my.setText(Html.fromHtml(文本)); 

code B

 的TextView我=(的TextView)findViewById(R.id.tVTitleOld); 。字符串文本= getResources()的getString(R.string.TitleOld);  my.setText(Html.fromHtml(文本));<字符串名称=TitleOld><字体颜色=#cc0029'>您好< / FONT>新<字体颜色=#FFCC00'>与世界LT; / FONT>< /串> 

解决方案

HTML code在的strings.xml 应该使用类似存储在CDATA

 <![CDATA [HTML源代码code]> 
labview中改变字符串的显示属性 字体大小 颜色

因此​​,简而言之您的code将

 <字符串名称=TitleOld><![CDATA [<字体颜色= '#cc0029 '>你好< / FONT>新<字体颜色= '#FFCC00 '>与世界LT; / FONT>]]>< /串> 

这是因为< 存储在XML文件中的&放大器; LT; > &放大器; GT; 等。 CDATA 维持性无论你需要显示,实际的文本

I hope to set single TextView with two different colored text, I have the read the document

Single TextView with two different colored text

Code A works well!

Code B get string from resource file, but the color of font can not be displayed, why?

Code A

 TextView my=(TextView) findViewById(R.id.tVTitleOld);
 String text = "<font color='#cc0029'>Hello</font> the <font color='#ffcc00'>world</font>";
 my.setText(Html.fromHtml(text));

Code B

 TextView my=(TextView) findViewById(R.id.tVTitleOld);
 String text = getResources().getString(R.string.TitleOld);
  my.setText(Html.fromHtml(text));


<string name="TitleOld"><font color='#cc0029'>Hello</font> the new <font color='#ffcc00'>world</font></string>

解决方案

HTML code in Strings.xml should be stored in CDATA using something like

<![CDATA[html source code]]>

So, in short your code would be

<string name="TitleOld"><![CDATA[<font color='#cc0029'>Hello</font> the new <font color='#ffcc00'>world</font>]]></string>

This is because < is stored in the xml file as &lt;, > as &gt; and so on. CDATA maintance the actual text whatever you need to display.

阅读全文

相关推荐

最新文章