Today, I suddenly found that the text layout in the Android project is very uneven and I have to solve it. After research, I finally found the cause of confusion caused by the automatic wrapping of textview-confusion between halfwidth and fullwidth characters! Generally, the entered numbers, letters, and punctuation marks are halfwidth, so the placeholder cannot be determined. They are very different from the placeholder characters of Chinese characters. For this reason, the layout of many texts is uneven. I have found two ways to solve this problem:
1. The characters in textview are fully divided. Convert all numbers, letters, and punctuation into full-width characters so that they share two bytes with Chinese characters. This can avoid typographical confusion caused by placeholder characters. The code for converting the halfwidth to fullwidth is as follows. You only need to call it.
Public static String ToSBC (String input) {// halfwidth to fullwidth: char [] c = input. toCharArray (); for (int I = 0; I <c. length; I ++) {if (c [I] = 32) {c [I] = (char) 12288; continue;} if (c [I] <127) c [I] = (char) (c [I] + 65248);} return new String (c );} /*** fullwidth conversion to halfwidth ** @ param input * @ return */public static String ToDBC (String input) {char [] c = input. toCharArray (); for (int I = 0; I <c. length; I ++) {if (c [I] = 12288) {c [I] = (char) 32; continue ;} if (c [I]> 65280 & c [I] <65375) c [I] = (char) (c [I]-65248 );} return new String (c );}
2. Remove special characters or replace all Chinese characters with English letters. Use a regular expression to filter all special characters, or replaceAll () to replace a Chinese character with an English character. After conversion, you can solve the layout confusion problem.
/*** Remove special characters or replace all Chinese characters with English letters ** @ param str * @ return */public static String stringFilter (String str) {str = str. replaceAll ("【","["). replaceAll ("]", "]"). replaceAll ("! ","! "). ReplaceAll (":", ":"); // Replace the Chinese character String regEx = "[" "]"; // clear the special character Pattern p = Pattern. compile (regEx); Matcher m = p. matcher (str); return m. replaceAll (""). trim ();}
Solve the layout of non-hierarchical la s:
Neat layout after solution, such:
Link: http://www.linuxidc.com/Linux/2012-02/54769.htm