TextView loaded hybrid character wrapping solution in Android

Source: Internet
Author: User

Yesterday the test to see a bug, as follows:

"3.1.0" When the survey is composed of Chinese and numerals, the first Chinese text will be automatically wrapped.

The final solution is to add this method:

 PrivateString Autosplittext (FinalTextView TV) {        FinalString Rawtext = Tv.gettext (). toString ();//Original Text        FinalPaint TVPaint = Tv.getpaint ();//paint, containing information such as fonts        Final floatTvwidth = Tv.getwidth ()-Tv.getpaddingleft ()-tv.getpaddingright ();//Control Available Width//split the original text by rowsstring[] Rawtextlines = Rawtext.replaceall ("\ R", ""). Split ("\ n")); StringBuilder Sbnewtext=NewStringBuilder ();  for(String rawtextline:rawtextlines) {if(Tvpaint.measuretext (Rawtextline) <=tvwidth) {                //if the entire row width is within the available width of the control, it is not processedsbnewtext.append (Rawtextline); } Else {                //if the entire row width exceeds the available width of the control, it is measured by character and wraps manually at the previous character beyond the available width                floatLineWidth = 0;  for(intCNT = 0; CNT! = Rawtextline.length (); ++CNT) {                    CharCH =Rawtextline.charat (CNT); LineWidth+=tvpaint.measuretext (string.valueof (ch)); if(LineWidth <=tvwidth)                    {sbnewtext.append (CH); } Else{sbnewtext.append ("\ n"); LineWidth= 0; --CNT; }}} sbnewtext.append ("\ n"); }        //get rid of the excess at the end.        if(!rawtext.endswith ("\ n") {Sbnewtext.deletecharat (Sbnewtext.length () )-1); }        returnsbnewtext.tostring (); }
View Code

After loading the call:

   Tvdes.settext (Desc.getsurvey_conclusion ());        Tvdes.settext (Autosplittext (tvdes));
View Code

If there is an unhealthy problem, call it in a different way:

   Viewtreeobserver observer = tvdes.getviewtreeobserver ();        Observer.addongloballayoutlistener (                new  Viewtreeobserver.ongloballayoutlistener () {                    @ Override                    publicvoid  ongloballayout () {                        tvdes.settext (Autosplittext (tvdes));                    }                });
View Code

You can do it.

TextView loaded hybrid character wrapping solution in Android

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.