Solve the Problem of automatic line feed of Android textview

Source: Internet
Author: User

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

Related Article

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.