String的几种事半功倍的用法

1.在标签中使用了‘(单引号)或者”(双引号)

这时可以使用\对其进行转译。
eg.

<string name="good_example">This\'ll work</string>
<string name="good_example">This is a \"good string\".</string>

2.使用String.format时,应该这样写

<string name="welcome_messages">Hello, %1$s! You have %2$d new messages.</string>
Resources res = getResources();
String text = String.format(res.getString(R.string.welcome_messages), "guchuanhang", 3);

3.使用Html标签

支持的html元素包括:
<b> for bold text.
<i> for italic text.
<u> for underline text.

    <string name="welcome">Welcome to <b>Android</b>! 
   String blankWelcome=getString(R.string.welcome);
        tv0.setText(Html.fromHtml(blankWelcome));

无效!(“Android”没有进行加粗)

注意: <在html中是一个特殊字符,需要使用&lt;进行转译

    <string name="welcome">Welcome to  &lt;b>Android &lt;b>!</string>

ok!

这里存在一个特殊的情况,如果既需要使用String.format,又需要使用html标签,而且参数中包含html中的特殊标签。这时可以使用TextUtils.htmlEncode(),对参数进行编码,不会当作html标签进行解析。
eg.

    <string name="welcome_messages_html">Hello, %1$s! You have &lt;b>%2$d new messages&lt;/b>.</string>
    String userName="guchuan<b>hang";
        String escapedUsername = TextUtils.htmlEncode(userName);
        Resources res = getResources();
        String text = String.format(res.getString(R.string.welcome_messages_html), userName, 3);
        CharSequence styledText = Html.fromHtml(text);
        tv0.setText(styledText);

4.使用Spannables修饰字符串样式

可以修改字体颜色/粗细/倾斜等。
才疏学浅,不误人子弟了,把代码贴一下,希望能够起到一个抛砖引玉的作用。

注意:如果使用+来连接普通字符和特殊字符的话,特殊字符的样式会抹掉,使用textView.append是ok的。

    Resources res=getResources();
        CharSequence textXXX = bold(italic(res.getString(R.string.app_name)),
                color(Color.RED, res.getString(R.string.app_name)));
        tv0.setText(textXXX);
  /** * Returns a CharSequence that concatenates the specified array of CharSequence * objects and then applies a list of zero or more tags to the entire range. * * @param content an array of character sequences to apply a style to * @param tags the styled span objects to apply to the content * such as android.text.style.StyleSpan * */
    private static CharSequence apply(CharSequence[] content, Object... tags) {
        SpannableStringBuilder text = new SpannableStringBuilder();
        openTags(text, tags);
        for (CharSequence item : content) {
            text.append(item);
        }
        closeTags(text, tags);
        return text;
    }

    /** * Iterates over an array of tags and applies them to the beginning of the specified * Spannable object so that future text appended to the text will have the styling * applied to it. Do not call this method directly. */
    private static void openTags(Spannable text, Object[] tags) {
        for (Object tag : tags) {
            text.setSpan(tag, 0, 0, Spannable.SPAN_MARK_MARK);
        }
    }

    /** * "Closes" the specified tags on a Spannable by updating the spans to be * endpoint-exclusive so that future text appended to the end will not take * on the same styling. Do not call this method directly. */
    private static void closeTags(Spannable text, Object[] tags) {
        int len = text.length();
        for (Object tag : tags) {
            if (len > 0) {
                text.setSpan(tag, 0, len, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            } else {
                text.removeSpan(tag);
            }
        }
    }
    /** * Returns a CharSequence that applies boldface to the concatenation * of the specified CharSequence objects. */
    public static CharSequence bold(CharSequence... content) {
        return apply(content, new StyleSpan(Typeface.BOLD));
    }

    /** * Returns a CharSequence that applies italics to the concatenation * of the specified CharSequence objects. */
    public static CharSequence italic(CharSequence... content) {
        return apply(content, new StyleSpan(Typeface.ITALIC));
    }

    /** * Returns a CharSequence that applies a foreground color to the * concatenation of the specified CharSequence objects. */
    public static CharSequence color(int color, CharSequence... content) {
        return apply(content, new ForegroundColorSpan(color));
    }

翻译地址:

https://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling

你可能感兴趣的:(String)