是否有可能为一个TextView内的不同文本块设置多个样式?

例如,我将文本设置如下:

tv.setText(line1 + "\n" + line2 + "\n" + word1 + "\t" + word2 + "\t" + word3);

是否可以为每个文本元素设置不同的样式?例如,行1加粗,字1斜体等。

开发者指南的常见任务和如何在Android中执行这些任务包括选择,突出显示或样式化文本部分:

// Get our EditText object. EditText vw = (EditText)findViewById(R.id.text); // Set the EditText's text. vw.setText("Italic, highlighted, bold."); // If this were just a TextView, we could do: // vw.setText("Italic, highlighted, bold.", TextView.BufferType.SPANNABLE); // to force it to use Spannable storage so styles can be attached. // Or we could specify that in the XML. // Get the EditText's internal text storage Spannable str = vw.getText(); // Create our span sections, and assign a format to each. str.setSpan(new StyleSpan(android.graphics.Typeface.ITALIC), 0, 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); str.setSpan(new BackgroundColorSpan(0xFFFFFF00), 8, 19, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); str.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 21, str.length() - 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

但这使用了文本中的显式位置编号。有更干净的方法吗?


当前回答

这可能就像利用String的length()方法一样简单:

将Strings XML文件中的文本字符串拆分为许多子字符串(从Android的角度来看是一个单独的字符串),因为你需要不同的风格,所以它可以像:str1, str2, str3(在你的情况下),当连接在一起时,就是你使用的整个单一字符串。 然后简单地遵循“Span”方法,就像您在代码中展示的那样——但不是单个字符串,而是将所有子字符串合并为单个字符串,每个子字符串具有不同的自定义样式。

你仍然使用这些数字,但不是直接使用——它们现在不再采用硬编码形式(就像在你的代码中那样),但它们被组合length()方法所取代(注意在str.length()之前和后面加上两颗星来代替绝对数字,以消除变化):

str.setSpan(new StyleSpan(android.graphics.Typeface.ITALIC), 0, **str.length()**, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

对于第一个字符串大小,则str.length() + 1,对于第二个字符串大小,则str.length() + str2.length(),等等,所有的子字符串,而不是例如0,7或8,19等等…

其他回答

我也遇到了同样的问题。我可以使用fromHtml,但我现在是android,不是web,所以我决定尝试一下。我必须本地化这个所以我用字符串替换的概念给了它一个机会。我在TextView上设置的风格是主要的风格,然后只是格式的其他作品。

我希望这能帮助其他人做同样的事情——我不知道为什么在这个框架中这不是更容易。

我的字符串是这样的:


<string name="my_text">{0} You will need a {1} to complete this assembly</string>
<string name="text_sub0">1:</string>
<string name="text_sub1">screwdriver, hammer, and measuring tape</string>

下面是一些款式:


<style name="MainStyle">
    <item name="android:textSize">@dimen/regular_text</item>
    <item name="android:textColor">@color/regular_text</item>
</style>
<style name="style0">
    <item name="android:textSize">@dimen/paragraph_bullet</item>
    <item name="android:textColor">@color/standout_text</item>
    <item name="android:textStyle">bold</item>
</style>
<style name="style1">
    <item name="android:textColor">@color/standout_light_text</item>
    <item name="android:textStyle">italic</item>
</style>

下面是调用formatStyles方法的代码:


SpannableString formattedSpan = formatStyles(getString(R.string.my_text), getString(R.string.text_sub0), R.style.style0, getString(R.string.main_text_sub1), R.style.style1);
textView.setText(formattedSpan, TextView.BufferType.SPANNABLE);

格式方法:


private SpannableString formatStyles(String value, String sub0, int style0, String sub1, int style1)
{
    String tag0 = "{0}";
    int startLocation0 = value.indexOf(tag0);
    value = value.replace(tag0, sub0);

    String tag1 = "{1}";
    int startLocation1 = value.indexOf(tag1);
    if (sub1 != null && !sub1.equals(""))
    {
        value = value.replace(tag1, sub1);
    }

    SpannableString styledText = new SpannableString(value);
    styledText.setSpan(new TextAppearanceSpan(getActivity(), style0), startLocation0, startLocation0 + sub0.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    if (sub1 != null && !sub1.equals(""))
    {
        styledText.setSpan(new TextAppearanceSpan(getActivity(), style1), startLocation1, startLocation1 + sub1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

    return styledText;
}

我也是

如何使用一些漂亮的标记与Kotlin和Anko -

import org.jetbrains.anko.*
override fun onCreate(savedInstanceState: Bundle?) {
    title = "Created with Beautiful Markup"
    super.onCreate(savedInstanceState)

    verticalLayout {
        editText {
            hint = buildSpanned {
                append("Italic, ", Italic)
                append("highlighted", backgroundColor(0xFFFFFF00.toInt()))
                append(", Bold", Bold)
            }
        }
    }
}

如果你想在xml中添加样式文本,你可以创建一个自定义视图扩展TextView并覆盖setText():

public class HTMLStyledTextView extends TextView
{
    public HTMLStyledTextView(Context context) {
        super(context);
    }

    public HTMLStyledTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public HTMLStyledTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public void setText(CharSequence text, BufferType type)
    {
       super.setText(Html.fromHtml(text.toString()), type);
    }
}

然后,你可以像这样使用它(用你的包名替换PACKAGE_NAME):

<PACKAGE_NAME.HTMLStyledTextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="<![CDATA[
        <b>Bolded Text:</b> Non-Bolded Text
    ]]>"
/>

是的,可以使用SpannedString。如果你正在使用Kotlin,使用core-ktx会变得更容易,因为它提供了一种领域特定语言(DSL)来做这件事:

    val string: SpannedString = buildSpannedString {
        bold {
            append("1111")
        }
        append("Devansh")     
    }

它提供的更多选项有:

append("Hello There")
bold {
    append("bold")
    italic {
        append("bold and italic")
        underline {
            append("then some text with underline")
        }
    }
}

最后,你只需:

textView.text = string

这可能就像利用String的length()方法一样简单:

将Strings XML文件中的文本字符串拆分为许多子字符串(从Android的角度来看是一个单独的字符串),因为你需要不同的风格,所以它可以像:str1, str2, str3(在你的情况下),当连接在一起时,就是你使用的整个单一字符串。 然后简单地遵循“Span”方法,就像您在代码中展示的那样——但不是单个字符串,而是将所有子字符串合并为单个字符串,每个子字符串具有不同的自定义样式。

你仍然使用这些数字,但不是直接使用——它们现在不再采用硬编码形式(就像在你的代码中那样),但它们被组合length()方法所取代(注意在str.length()之前和后面加上两颗星来代替绝对数字,以消除变化):

str.setSpan(new StyleSpan(android.graphics.Typeface.ITALIC), 0, **str.length()**, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

对于第一个字符串大小,则str.length() + 1,对于第二个字符串大小,则str.length() + str2.length(),等等,所有的子字符串,而不是例如0,7或8,19等等…