A strange question for spannablestring.

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.