我有 3 个字符串本地化
<string name="tests" formatted="true">Test<annotation font="bold"> testBold %1$s</annotation> end</string>
<string name="tests" formatted="true">Тест<annotation font="bold"> тестБолд %1$s</annotation> конец</string>
<string name="tests" formatted="true">Тест<annotation font="bold"> тестБолд %1$s</annotation> кінець</string>
那么我如何通过注释添加一些参数和修改后的文本。我得到的最大收获就是做这件事
CharSequence t = getResources().getString(R.string.tests, "myValue");//in this case i lose my annotation, but set my argument
//OR
CharSequence t = getText(R.string.tests);//in this case i lose my argument but get style BOLD
public SpannableString textFormattingByTags(CharSequence t) {
SpannedString titleText = new SpannedString(t);
SpannedString titleText = (SpannedString) getText(R.string.tests);
Annotation[] annotations = titleText.getSpans(0, titleText.length(), Annotation.class);
SpannableString spannableString = new SpannableString(titleText);
for (Annotation annotation : annotations) {
if (annotation.getKey().equals("font")) {
String fontName = annotation.getValue();
if (fontName.equals("bold")) {
spannableString.setSpan(new CustomTypefaceSpan("",fontBold),
titleText.getSpanStart(annotation),
titleText.getSpanEnd(annotation),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
}
return spannableString;
}
结果在第一种情况下,我在第二个“Test testBold %1$s end”中得到“Test testBold MyValue end”。谁有一些想法?