如何使用SpannableString
中定义的字符串设置strings.xml
?
例如,我有:%s liked your post
然后我做
SpannableString usernameSS = new SpannableString(username);
usernameSS.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), 0, username.length(), 0);
getString(R.string.like_post, usernameSS);
但它不起作用 - 风格没有得到应用,我知道我可以使用HTML <b><i>
但是有没有办法做到这一点而不必诉诸于HTML?因为我可能需要稍后通过TextAppearanceSpan
答案 0 :(得分:3)
String finalString = getString(R.string.like_post, username);
SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(finalString);
int startIndex = finalString.indexOf(username);
spannableStringBuilder.setSpannable(new StyleSpan(Typeface.BOLD_ITALIC), startIndex, startIndex + username.length(), 0);
应该这样做。请检查正确的开始/结束索引