Frage

final StyleSpan bss = new StyleSpan(android.graphics.Typeface.BOLD);
final SpannableString ss = new SpannableString("How to " + text + " in " + type);
ss.setSpan(bss, 7, text.length() + 7, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
ss.setSpan(bss, 7 + text.length() + 4, ss.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
textView.setText(ss);

I want to make text and type BOLD. But only type is BOLD.

What did I miss?

War es hilfreich?

Lösung

According to the documentation :

setSpan(Object what, int start, int end, int flags) Attach the specified markup object to the range start…end of the text, or move the object to that range if it was already attached elsewhere.

A StyleSpan can only be used once in a Spannable. You need create a StyleSpan for each text and type

Andere Tipps

You should create object like below.

ss.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 7, text.length() + 7, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
ss.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 7 + text.length() + 4, ss.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
textView.setText(ss)

A better solution is to use the static method instead of create several Objects:

CharacterStyle.wrap(CharacterStyle c)
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top