2011-09-12 14 views
70

Czy można ustawić różne rozmiary tekstów w jednym widoku tekstowym? Wiem, że mogę zmienić styl tekstu, używając:TextView z innym tekstemRozmiar

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

Znam początek zakresu ... koniec tekstu Chcę zmienić rozmiar. Ale co mogę użyć jako arg0 i arg3?

Odpowiedz

164

spróbować

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

To wygląda poprawnie, ale nie działa. Cały tekst w moim TextView ma taki sam rozmiar przez cały czas. – woyaru

+0

Hmm może powinienem zastosować (zaktualizować) rozpiętość do mojego TextView po setSpan()? – woyaru

+3

Przepraszamy za 3 komentarze, ale rozwiązałem ten problem. Po prostu: 'textView.setText (span)'. – woyaru

16

Znam bardzo późno, by odpowiedzieć, ale ludzie wciąż mogą mieć taką samą question.Even Walczyłem wiele w tej sprawie. Załóżmy, że masz te dwa ciągi wewnątrz pliku 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> 

Teraz trzeba zdefiniować dwa styl dla nich wewnątrz style.xml z innym 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> 

Teraz z Java plik, który trzeba użyć spannable załadować te dwa style i sznurki na pojedynczym 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); 

Poniżej jest metoda formatStyles który zwróci sformatowaną strln g po zastosowaniu stylu

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

Spróbuj z AbsoluteSizeSpan

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

Kompletny kod jest:

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