String truncation Assembly wrap approaching Textview wrap

Source: Internet
Author: User

String truncation Assembly wrap approaching Textview wrap

Abnormal requirements Textview can only display line breaks with more than 10 English characters. Default android Textview can only be mandatory line breaks of width, or is there a linefeed "/n" in the String, but what if there is no linefeed for the String?

Here, we need to modify it as determined by ourselves.

// Set the name

String test = "abcdefg12345678"

TextView textView = (TextView) findViewById (R. id. TV _test );
TextView. setText (toMultiLine (test, 3 ));

 

/** String truncation line feed
* @ Param str
* @ Param len
* @ Return
*/
Private String toMultiLine (String str, int len ){
Char [] chs = str. toCharArray ();
StringBuffer sb = new StringBuffer ();
For (int I = 0, sum = 0; I <chs. length; I ++ ){
Sum + = chs [I] <0xff? 1: 2;
Sb. append (chs [I]);
If (sum> = len ){
Sum = 0;
Sb. append ("\ n ");
}
}
Return sb. toString ();
}

Result:

Abc
Def
G12
345
678

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.