首页 > 解决方案 > Why can't the color of a string from resource file be changed in Android?

问题描述

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

enter image description here

   <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)

标签: androidkotlin

解决方案


尝试这个

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

比这样使用

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));
}

输出

在此处输入图像描述


推荐阅读