通过参数

时间:2016-06-03 12:47:14

标签: android xml string spannablestring

我在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个字符串参数:namepermissionphoto

我有两种设置不同风格的方法。

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解决方案也是另一种方式。

2 个答案:

答案 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);
    }