Warm tip: This article is reproduced from stackoverflow.com, please click
android android-layout xml android-studio

Color part of string in textView in XML

发布于 2020-03-27 10:31:43

I am trying to display some text in a TextView with @string, the problem is i cant seem to be able to change the color of part of the text. I dont want to use Java to do this as all other questions suggest, since this is the default TextView text.

I've tried using <string name="myString"><font color="#00ff00">My String</font></string> and font fgcolor="#00ff00" but they dont seem to work.

Questioner
kriskotoo BG
Viewed
74
2,820 2019-07-04 03:22

I've tried with your given string and it is works for me. Here is an example

  • Create <TextView> inside any layout

    <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/text" />

  • Create text string inside strings.xml file

    <string name="text"><font color="#00ff00">Hello</font> <font color="#FF0000">World</font></string>

  • And you can see the result

    enter image description here