实现如下效果:

Hello! 小傻You have 5 new messages.

  1. string.xml

    <string name="welcome_messages"><![CDATA[Hello! <font color="#ff0000">%1$s</font>You have %2$d new messages.]]></string>

    注意html中的引号,如果用单引号就要加转译字符\,为了方便就用双引号吧。

  2. 在Activity中动态获取并设置

    1
    2
    3
    4
    Resources resources = getResources();
    String htmlStr = resources.getString(R.string.welcome_messages,"小傻",5);
    Spanned spannedText = Html.fromHtml(htmlStr);
    textView.setText(spannedText);

其实上面的代码可以写成一行:
textView.setText(Html.fromHtml(getResources().getString(R.string.welcome_messages,"小傻",5)));