我希望成立一个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]>
因此,简而言之您的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 <
, >
as >
and so on. CDATA
maintance the actual text whatever you need to display.
相关推荐
最新文章