Today, the use of spannablestring encountered a strange problem, that is, when Setspan, can be written
1 spannablestring.setspan (new Relativesizespan (0.5f), Stringsize + 2,2 2 + Stringsize + 1, spannable.span_inclusive_exclusive); 3 Spannablestring.setspan (new Foregroundcolorspan (color.ltgray),4 stringsize + 2, 2 + stringsize + 1,5 spannable.span_inclusive_exclusive); 6 Textview_next_distance.settext (spannablestring);
This form, note that there is an object at the back of the Setspan that sets the font size (new Relativesizespan (0.5)), and sets the text color (new Foregroundcolorspan (Color.ligray)) The object. Since there is a lot of text behind this font size and color, I'm instantiating this object into member variables, uniformly used and modified.
1 Private New Relativesizespan (0.5f); 2 Private New Foregroundcolorspan (3 Color.ltgray);
The problem arises, however, that when you use the same member variable two times in the same TextView, the previous changed text is invalid.
The correct wording should be the first one to use the member variable, if there is a second or a third then new, the following is no problem
1 //Road Guide, the remaining distance of the section2String strdistance = naviinfo.getcurstepretaindistance () + "";3Stringsize =strdistance.length ();4Spannablestring =NewSpannablestring ("front" + strdistance + "M"5+Consutil.stractions[naviinfo.m_icon]);6Spannablestring.setspan (Relativesizespan, 0, 2,7 spannable.span_inclusive_exclusive);8Spannablestring.setspan (Foregroundcolorspan, 0, 2,9 spannable.span_inclusive_exclusive);TenSpannablestring.setspan (NewRelativesizespan (0.5f), Stringsize + 2, One2 + stringsize + 1, spannable.span_inclusive_exclusive); ASpannablestring.setspan (NewForegroundcolorspan (Color.ltgray), -Stringsize + 2, 2 + stringsize + 1, - spannable.span_inclusive_exclusive); theTextview_next_distance.settext (spannablestring);
Effect
The wrong thing to do is
1 //Road Guide, the remaining distance of the section2String strdistance = naviinfo.getcurstepretaindistance () + "";3Stringsize =strdistance.length ();4Spannablestring =NewSpannablestring ("front" + strdistance + "M"5+Consutil.stractions[naviinfo.m_icon]);6Spannablestring.setspan (Relativesizespan, 0, 2,7 spannable.span_inclusive_exclusive);8Spannablestring.setspan (Foregroundcolorspan, 0, 2,9 spannable.span_inclusive_exclusive);TenSpannablestring.setspan (Relativesizespan, Stringsize + 2, One2 + stringsize + 1, spannable.span_inclusive_exclusive); A Spannablestring.setspan (Foregroundcolorspan, -Stringsize + 2, 2 + stringsize + 1, - spannable.span_inclusive_exclusive); theTextview_next_distance.settext (spannablestring);
As a result, only the following style is valid, and the previous auto revert to the default style
As to why this is not very clear, this need to study the source code to know, now in overtime, if later found out, follow-up I will make up the reason.
My github Address: https://github.com/dongweiq/study
Welcome attention, Welcome to star O (∩_∩) O. What's the problem please contact [email protected] qq714094450
A strange question for spannablestring.