Why can't the color of a string from resource

2020-04-17 04:27发布

问题:

tvName is TextView control, the color of "World" has changed, but color of "Paul" hasn't changed, why?

And more, in Android Studio 3.1.3, I get the prompt "'fromHtml(String!): Spanned!' is deprecated. Deprecated in Java" , how can I fix it? Now I use Android Studio 3.1.3 with Kotlin 1.2.50

Image

   <string name="aa">
        Hello <font color='#0000FF'>Paul </font> !
    </string>


    val s=mContext.getString(R.string.aa)+" Hello <font color='#0000FF'>World </font>"    
    tvName.text= Html.fromHtml(s)

回答1:

Try this

<string name="aa">Hello <![CDATA[<font color=#0000FF>Paul!</font>]]></string>

Than use like this

String value=getString(R.string.aa)+" Hello <font color='#0000FF'>World </font>";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    myTextView.setText(Html.fromHtml(value,Html.FROM_HTML_MODE_LEGACY));
 }else {
    myTextView.setText(Html.fromHtml(value));
}

OUTPUT