2011-09-12 16 views
70

1つのTextViewに異なるtextSizeを設定できますか?異なるtextSizeのTextView

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

を私はサイズを変更するテキストの範囲開始...終了を知っている:私は、私が使用してテキストのスタイルを変更することができることを知っています。でも、arg0arg3として何が使えますか?

答えて

164

は私が答えることは非常に遅れて知っているが、人々はまだある私は、この上でたくさん を苦労同じquestion.Evenがあるかもしれません

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

それが正常に見えるが、動作しません。私のTextViewの全文は常に同じサイズです。 – woyaru

+0

おそらく、私はsetSpan()の後にTextViewにスパンを適用(更新)する必要がありますか? – woyaru

+3

私の3つのコメントに申し訳ありませんが、私はこの問題を解決しました。ただ: 'textView.setText(span)'。 – woyaru

16

を試してみてください。 はあなたが今、あなたのJavaファイルから今

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

異なるTEXTSIZEであなたのstyle.xml内で彼らのためにあなたを2つのスタイルを定義する必要があり、あなたの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> 

を持っていると仮定します単一の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); 

フォーマットさstrinを返しますformatStyles方法であるが、これらの2つのスタイルと文字列をロードするためにspannableを使用する必要がありますグラムスタイルを適用した後

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

AbsoluteSizeSpan

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

完全なコードがあるとしてみてください。

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();` 
関連する問題