por*_*der 2 string android colors
我找到了一些关于如何更改存储在strings.xml中的字符串的特定字符和单词的颜色的解决方案,但是,它对我不起作用.我有以下字符串
<string name="my_string">
Humpty Dumpty sat on a <font fgcolor="#FF0000">wall</font> Humpty Dumpty had a great <font fgcolor="#FF0000">fall</font></string>
Run Code Online (Sandbox Code Playgroud)
我期待着墙壁和秋天的颜色是#FF0000(红色)的颜色,然而,它们根本不显示.经过进一步的研究,我了解到你必须声明alpha通道,然后我将我的字符串更新为
<string name="my_string">
Humpty Dumpty sat on a <font fgcolor="#FFFF0000">wall</font> Humpty Dumpty had a great <font fgcolor="#FFFF0000">fall</font></string>
Run Code Online (Sandbox Code Playgroud)
我声明的alpha通道完全不透明,但结果保持不变.
最后,我也尝试过使用CDDATA,这是我的尝试.
<string name="my_string"><![CDATA[
Humpty Dumpty sat on a <font color=\'#FF0000\'>wall</font> Humpty Dumpty had a great <font color=\'#FF0000\'>fall</font>]]</string>
Run Code Online (Sandbox Code Playgroud)
还有什么我做错了?提前致谢!
你想要使用这样的东西.
textView.setText(Html.fromHtml(getString(R.string.some_text)));
Run Code Online (Sandbox Code Playgroud)
你strings.xml应该包含如下字符串.
<string name="some_text"><![CDATA[<font color=\'#cc0029\'>Some</font> random <font color=\'#ffcc00\'>text</font>]]></string>
Run Code Online (Sandbox Code Playgroud)
点击此处了解更多详情.