TextView com textSize diferente

88

É possível definir textSize diferente em um TextView? Eu sei que posso mudar o estilo do texto usando:

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

Eu sei o intervalo de início ... fim do texto, quero alterar o tamanho. Mas o que posso usar como arg0e arg3?

woyaru
fonte

Respostas:

198

Tentar

span.setSpan(new RelativeSizeSpan(0.8f), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Michele
fonte
1
Parece bem, mas não funciona. Todo o texto em meu TextView tem o mesmo tamanho o tempo todo.
woyaru
1
Hmm, talvez eu deva aplicar (atualizar) o intervalo ao meu TextView após setSpan ()?
woyaru
4
Desculpe pelos meus 3 comentários, mas eu resolvi esse problema. Assim: textView.setText(span).
woyaru
você sabe como posso combinar tamanho com cor para extensão?
Ionut Negru
23

Sei muito tarde para responder, mas as pessoas ainda podem ter a mesma pergunta. Até eu lutei muito nisso. Suponha que você tenha essas duas strings dentro do seu arquivo 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>

Agora você precisa definir dois estilos para eles dentro de seu style.xml com 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>

Agora, a partir do seu arquivo Java, você precisa usar o spannable para carregar esses dois estilos e strings em um único 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);

Abaixo está o método formatStyles que irá retornar a string formatada após aplicar o 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;
}
John
fonte
12

Experimente com AbsoluteSizeSpan

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

O código 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();`
Simone Dagli Orti
fonte