gpt4 book ai didi

android - 通过参数将 Spannable 应用于 string.xml 中的字符串

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:45:34 25 4
gpt4 key购买 nike

我在 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;
}

我知道我们可以将 SpannableTextUtils.concat(...) 连接起来,但我的情况不同。

解决方案

请检查以下解决方案。 @Krislarson 解决方案也是另一种方法。

最佳答案

下面是如何使用 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;
}

关于android - 通过参数将 Spannable 应用于 string.xml 中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37614801/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com