另外对于带风格/格式的string的处理,就麻烦一点点。官方给了一个例子
:
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="search_results_resultsTextFormat">%1$d results for <b>&quot;%2$s&quot;</b></string>
- </resources>
这里的%1$d是个十进制数字,%2$s是字符串。当我们把某个字符串赋值给%2$s之前,需要用htmlEncode(String)函数处理那个字符串:
- //title是我们想赋值给%2$s的字符串
- String escapedTitle = TextUtil.htmlEncode(title);
然后用String.format() 来实现赋值,接着用fromHtml(String) 得到格式化后的string:
- String resultsTextFormat = getContext().getResources().getString(R.string.search_results_resultsTextFormat);
- String resultsText = String.format(resultsTextFormat, count, escapedTitle);
- CharSequence styledResults = Html.fromHtml(resultsText);