通过参数将 Spannable 应用于 string.xml 中的字符串
Apply Spannable to string in string.xml through parameter
我在 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>
字符串接受 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;
}
现在我将这些样式应用于不同的参数和 return 字符串,但这不起作用。这是我正在做的事情:
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 解决方案也是另一种方法。
以下是如何使用 SpannableStringBuilder
执行此操作。请注意,return 类型已更改为 CharSequence
以适应构建的 Spannable
字符串。您可以将 return 值直接传递给 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;
}
我需要 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);
}
我在 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>
字符串接受 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;
}
现在我将这些样式应用于不同的参数和 return 字符串,但这不起作用。这是我正在做的事情:
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 解决方案也是另一种方法。
以下是如何使用 SpannableStringBuilder
执行此操作。请注意,return 类型已更改为 CharSequence
以适应构建的 Spannable
字符串。您可以将 return 值直接传递给 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;
}
我需要 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);
}