2011-09-12 22 views
70

¿Es posible establecer diferentes TextSize en una TextView? Sé que puedo cambiar de estilo de texto usando:TextView con diferente textSize

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

sé que el área de alcance ... al final del texto que desea cambiar de tamaño. Pero, ¿qué puedo usar como arg0 y arg3?

Respuesta

164

tratar

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

se ve correctamente, pero no funciona. Todo el texto en mi TextView tiene el mismo tamaño todo el tiempo. – woyaru

+0

Hmm tal vez debería aplicar (actualizar) el lapso a mi TextView después de setSpan()? – woyaru

+3

Lo siento por mis 3 comentarios pero he resuelto este problema. Solo: 'textView.setText (span)'. – woyaru

16

Sé muy tarde para responder, pero la gente todavía podría tener la misma question.Even me ha costado un montón en esto. Suponga que tiene estas dos cadenas dentro de su archivo 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> 

Ahora tendrá que definir dos Estilo de ellos dentro de su style.xml con diferentes 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> 

Ahora desde su archivo de Java necesitará utilizar el spannable para cargar estos dos estilo y cuerdas con sencillo en el 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); 

a continuación se muestra el método formatStyles que devolverá el formato strin g después de aplicar el estilo

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

intento con AbsoluteSizeSpan

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

El código completo es:

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