मैं जवाब देने के लिए बहुत देर हो चुकी है, लेकिन लोगों को अभी भी कर रहे हैं एक ही 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;
}
यह ठीक से दिखता है, लेकिन काम नहीं करता। मेरे टेक्स्ट व्यू में पूरा टेक्स्ट हर समय एक ही आकार का होता है। – woyaru
हम्म शायद मुझे setSpan() के बाद मेरे टेक्स्ट व्यू में अवधि (अपडेट) लागू करनी चाहिए? – woyaru
मेरी 3 टिप्पणियों के लिए खेद है, लेकिन मैंने इस समस्या का समाधान किया है। बस: 'textView.setText (अवधि) '। – woyaru