我在string.xml
<string name="photo_heading"><xliff:g name="name">%s</xliff:g> shared a <xliff:g name="permission">%s</xliff:g><xliff:g name="photo">%s</xliff:g> with you.</string>
String接受3个字符串参数:name
,permission
,photo
我有两种设置不同风格的方法。
public Spannable getStyledItalic(String text) {
Spannable sb = new SpannableString(text);
sb.setSpan(new StyleSpan(Typeface.ITALIC), 0, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
return sb;
}
和
public Spannable getStyledName(String name) {
Spannable sb = new SpannableString(name);
sb.setSpan(new AbsoluteSizeSpan(LPConstants.FONT_SIZE_NAME), 0, name.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
sb.setSpan(new StyleSpan(Typeface.BOLD), 0, name.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
sb.setSpan(new ForegroundColorSpan(mContext.getResources().getColor(R.color.app_primary)), 0, name.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
return sb;
}
现在我将这些样式应用于不同的参数并返回字符串,但这不起作用。这就是我在做的事情:
public String getHeading(Printrequest printRequest) {
String name = printRequest.sentbyname;
String permission;
String photo;
if (printRequest.mediaobject.permission != null && !printRequest.mediaobject.permission.equalsIgnoreCase("public")) {
permission = printRequest.mediaobject.permission;
} else {
permission = "";
}
if (printRequest.mediaobject.hyperphoto != null) {
photo = " " + getStyledItalic(mContext.getString(R.string.hyperphoto));
} else {
photo = " " + mContext.getString(R.string.photo);
}
String heading = mContext.getString(R.string.photo_heading, getStyledName(name), getStyledItalic(permission), photo);
return heading;
}
我知道我们可以将Spannable
与TextUtils.concat(...)
连接起来,但我的情况有所不同。
解
请检查以下解决方案。 @Krislarson解决方案也是另一种方式。
答案 0 :(得分:4)
以下是使用SpannableStringBuilder
执行此操作的方法。请注意,返回类型已更改为CharSequence
以容纳构建的Spannable
字符串。您可以将返回值直接传递给TextView.setText()
。
public CharSequence getHeading(Printrequest printRequest) {
String name = printRequest.sentbyname;
String permission;
if (printRequest.mediaobject.permission != null && !printRequest.mediaobject.permission.equalsIgnoreCase("public")) {
permission = printRequest.mediaobject.permission;
} else {
permission = "";
}
String photoHeading = mContext.getString(R.string.photo_heading);
SpannableStringBuilder builder = new SpannableStringBuilder(photoHeading);
int pos = builder.toString().indexOf("%s");
builder.replace(pos, pos+2, getStyledName(name));
pos = builder.toString().indexOf("%s");
builder.replace(pos, pos+2, getStyledItalic(permission));
pos = builder.toString().indexOf("%s");
if (printRequest.mediaobject.hyperphoto != null) {
builder.replace(pos, pos+2, getStyledItalic(mContext.getString(R.string.hyperphoto)));
builder.insert(pos, " ");
} else {
builder.replace(pos, pos+2, mContext.getString(R.string.photo));
builder.insert(pos, " ");
}
return builder;
}
答案 1 :(得分:1)
我需要TextUtils.expandTemplate(...)
。感谢 @pskink 指出, @krislarson 表示他的努力。
<string name="photo_heading"><xliff:g name="name">^1</xliff:g> shared a<xliff:g name="permission">^2</xliff:g> <xliff:g name="photo">^3</xliff:g> with you.</string>
和
public CharSequence getHeading(Printrequest printRequest) {
String name = printRequest.sentbyname;
String permission;
CharSequence photo;
if (printRequest.mediaobject.permission != null && !printRequest.mediaobject.permission.equalsIgnoreCase("public")) {
permission = " " + printRequest.mediaobject.permission;
} else {
permission = "";
}
if (printRequest.mediaobject.hyperphoto != null) {
photo = getStyledItalic(mContext.getString(R.string.hyperphoto));
} else {
photo = mContext.getString(R.string.photo);
}
return TextUtils.expandTemplate(mContext.getString(R.string.photo_heading), getStyledName(mContext, name), getStyledItalic(permission), photo);
}