2011-09-12 18 views
70

È possibile impostare un valore di testo diverso in una TextView? So che posso cambiare lo stile del testo usando:TextView con testo differente Dimensione

TextView textView = (TextView) findViewById(R.id.textView); 
Spannable span = new SpannableString(textView.getText()); 
span.setSpan(arg0, 1, 10, arg3); 
textView.setText(span) 

Conosco l'intervallo inizio ... fine del testo Voglio cambiare dimensione. Ma cosa posso usare come arg0 e arg3?

risposta

164

provare

span.setSpan(new RelativeSizeSpan(0.8f), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
+0

E sembra correttamente, ma non funziona. L'intero testo nel mio TextView ha sempre le stesse dimensioni. – woyaru

+0

Hmm forse dovrei applicare (aggiornare) lo span al mio TextView dopo setSpan()? – woyaru

+3

Ci scusiamo per i miei 3 commenti ma ho risolto questo problema. Semplicemente: 'textView.setText (span)'. – woyaru

16

conosco molto tardi per rispondere, ma le persone sono ancora potrebbero avere la stessa question.Even ho faticato molto su questo. Supponiamo di avere queste due stringhe all'interno del vostro file di strings.xml

<string name="my_text">You will need a to complete this assembly</string> 
<string name="text_sub1">screwdriver, hammer, and measuring tape</string> 

A questo punto è necessario definire due Style per loro all'interno del vostro style.xml con diversi textSize

<style name="style0"> 
    <item name="android:textSize">19sp</item> 
    <item name="android:textColor">@color/standout_text</item> 
    <item name="android:textStyle">bold</item> 
</style> 
<style name="style1"> 
    <item name="android:textSize">23sp</item> 
    <item name="android:textColor">@color/standout_light_text</item> 
    <item name="android:textStyle">italic</item> 
</style> 

Ora dal Java si file necessario utilizzare il spannable per caricare questi due stile e archi su un singolo textView

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); 

di seguito è riportato il metodo formatStyles che restituirà la strin formattato g dopo l'applicazione dello stile

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; 
} 
1

Prova con AbsoluteSizeSpan

snackbarText.setSpan(new AbsoluteSizeSpan(fontsize, true), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 

il codice completo è:

SpannableStringBuilder snackbarText = new SpannableStringBuilder(); 
snackbarText.append("Your text"); 
snackbarText.setSpan(new AbsoluteSizeSpan(fontsize, true), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
Snackbar.make(getCurrentFocus(), snackbarText, Snackbar.LENGTH_LONG).setAction("Action", null).show();`