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