问题
<string name="some_text">this is <font fgcolor="#ffff0000">red</font></string>
or
<string name="some_text">this is <font color="#ffff0000">red</font></string>
is not doing it.
I need to call the string in an xml TextView. How do I get it to work? No I don't want to use a horizontal LinearLayout with a bunch of TextViews.
回答1:
You can't do this. instead of you can set TextView
text as HTML
like
textview.setText(Html.fromHtml("this is<font color=\"##ffff0000\">red</font>"));
Go to this for more HTML
tag support information :html-tags-supported-by-textview
回答2:
TextView doesn't support entering HTML text directly, though some of the other answers seem to show you how to get a Html
object from the String. If you want to format the text within a TextView you need to use Spannable
instances to format the sections of text to have different colours, fonts etc. This is only good if you don't already have your text in HTML though, of course.
来源:https://stackoverflow.com/questions/22118558/color-string-resource-for-xml-layout