从
docs:
Sometimes you may want to create a styled text resource that is also
used as a format string. Normally, this won’t work because the
String.format(String, Object…) method will strip all the style
information from the string. The work-around to this is to write the
HTML tags with escaped entities, which are then recovered with
fromHtml(String), after the formatting takes place.
例如:
Hello, %1$s! You have <b>%2$d new messages</b>.
确保转义您在String.format()中传递的文本
String escapedUsername = TextUtils.htmlEncode(username);
Resources res = getResources();
String text = String.format(res.getString(R.string.welcome_messages), escapedUsername, mailCount);
CharSequence styledText = Html.fromHtml(text);