Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
524 views
in Technique[技术] by (71.8m points)

android - Text view with different colored texts in xml code

I need my textview to have different colored texts. Also I need to do this from xml code, non from java code. Is there anyone who knows some way for doing this? Thanks

e.g. I have sentence "This is red". I need words to be green, and word red to be red.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Refer your text to string.xml and using html font tag , by using that way you can change each letter color also .

just add this in java for that string:

  TextView tv=(TextView)findViewById(R.id.tv);

  tv.setText(Html.fromHtml(getString(R.string.any_text)));

and

In string.xml:

 <string name="any_text">
 <![CDATA[ <b><font color=#ff0000>write</b> your <b><font color=#0000ff>text</b> here .

]]> 
  </string>

hope help you


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...