2011-09-12 13 views
70

क्या एक टेक्स्ट व्यू में अलग-अलग टेक्स्ट आकार सेट करना संभव है? मुझे पता है कि मैं का उपयोग कर पाठ शैली बदल सकते हैं:टेक्स्टव्यू अलग-अलग पाठ के साथ

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

मैं रेंज शुरू पता है ... पाठ के अंत मैं आकार बदलना चाहते हैं। लेकिन मैं arg0 और arg3 के रूप में क्या उपयोग कर सकता हूं?

उत्तर

164

कोशिश

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

यह ठीक से दिखता है, लेकिन काम नहीं करता। मेरे टेक्स्ट व्यू में पूरा टेक्स्ट हर समय एक ही आकार का होता है। – woyaru

+0

हम्म शायद मुझे setSpan() के बाद मेरे टेक्स्ट व्यू में अवधि (अपडेट) लागू करनी चाहिए? – woyaru

+3

मेरी 3 टिप्पणियों के लिए खेद है, लेकिन मैंने इस समस्या का समाधान किया है। बस: 'textView.setText (अवधि) '। – woyaru

16

मैं जवाब देने के लिए बहुत देर हो चुकी है, लेकिन लोगों को अभी भी कर रहे हैं एक ही question.Even हो सकता है मैं इस पर बहुत कुछ संघर्ष किया। मान लीजिए कि आपके 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> 

अब आप अलग textSize के साथ अपने style.xml अंदर उनके लिए दो शैली परिभाषित करने की जरूरत

<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> 
अपने जावा से

अब आप फाइल एकल 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); 

नीचे पर इन दोनों शैली और तार लोड करने के लिए spannable उपयोग करने की आवश्यकता formatStyles विधि है जो स्वरूपित strin वापस आ जाएगी है शैली लागू करने के बाद ग्राम

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();` 
संबंधित मुद्दे