2013-02-20 13 views
33

Supponiamo che io ho il testo seguente:Come impostare più campate su un testo di TextView sullo stesso testo parziale?

Ciao StackOverflow

E vorrei per impostare la seconda parola ad essere sia RelativeSizeSpan (per impostare una dimensione del carattere relativa) e TextAppearanceSpan (per impostare il colore del testo), come li unisco entrambi?

Tutto quello che so è che posso scegliere uno di loro, utilizzando il codice successivo per esempio:

final SpannableString textToShow = new SpannableString("Hello stackOverflow"); 
textToShow.setSpan(new RelativeSizeSpan(1.5f), textToShow.length() - "stackOverflow".length(),textToShow.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
textView.setText(textToShow); 

ma ho bisogno di impostare anche il colore, o anche aggiungere altre funzionalità da altre classi che abbracciano ..

Cosa posso fare?

+0

puoi anche https://stackoverflow.com/a/41953808 – Suragch

risposta

63

Basta impostare ulteriori span. Stanno andando a sovrapporsi/unire quando necessario. Questo codice funziona per me:

final SpannableString text = new SpannableString("Hello stackOverflow"); 
text.setSpan(new RelativeSizeSpan(1.5f), text.length() - "stackOverflow".length(), text.length(), 
      Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
text.setSpan(new ForegroundColorSpan(Color.RED), 3, text.length() - 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
tv.setText(text); 
+0

qual è il "3" per? –

+6

numero casuale che avevo nella mia testa. È qui solo per mostrare che è possibile avere più span che si intersecano tra loro. – Zielony

+0

questa è di gran lunga la risposta più veloce che ho ricevuto e selezionata come quella corretta. congratulazioni ! –

27

So che questa una nuova risposta a una domanda già risposto, ma mi piacerebbe condividere una classe di utilità che ho fatto che rende questo compito più facile.

public class SimpleSpanBuilder { 
    private class SpanSection{ 
     private final String text; 
     private final int startIndex; 
     private final CharacterStyle[] styles; 

     private SpanSection(String text, int startIndex,CharacterStyle... styles){ 
      this.styles = styles; 
      this.text = text; 
      this.startIndex = startIndex; 
     } 

     private void apply(SpannableStringBuilder spanStringBuilder){ 
      if (spanStringBuilder == null) return; 
      for (CharacterStyle style : styles){ 
       spanStringBuilder.setSpan(style, startIndex, startIndex + text.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE); 
      } 
     } 
    } 

    private List<SpanSection> spanSections; 
    private StringBuilder stringBuilder; 

    public SimpleSpanBuilder(){ 
     stringBuilder = new StringBuilder(); 
     spanSections = new ArrayList<>(); 
    } 

    public SimpleSpanBuilder append(String text,CharacterStyle... styles){ 
     if (styles != null && styles.length > 0) { 
      spanSections.add(new SpanSection(text, stringBuilder.length(),styles)); 
     } 
     stringBuilder.append(text); 
     return this; 
    } 

    public SimpleSpanBuilder appendWithSpace(String text,CharacterStyle... styles){ 
     return append(text.concat(" "),styles); 
    } 

    public SimpleSpanBuilder appendWithLineBreak(String text,CharacterStyle... styles){ 
     return append(text.concat("\n"),styles); 
    } 

    public SpannableStringBuilder build(){ 
     SpannableStringBuilder ssb = new SpannableStringBuilder(stringBuilder.toString()); 
     for (SpanSection section : spanSections){ 
      section.apply(ssb); 
     } 
     return ssb; 
    } 

    @Override 
    public String toString() { 
     return stringBuilder.toString(); 
    } 
} 

Usage:

SimpleSpanBuilder ssb = new SimpleSpanBuilder(); 
ssb.appendWithSpace("Hello"); 
ssb.append("StackOverflow",new ForegroundColorSpan(Color.RED),new RelativeSizeSpan(1.5)); 
textView.setText(ssb.build()); 
+5

Questa è in realtà una bella soluzione. +1 per lo sforzo. –

+0

come impostare click listener sui testi –

+0

@PrinkalKumar Prova la risposta aggiornata con 'ClickableSpan' –

Problemi correlati