On the problem of text alignment and line breaking in Android textview

Today, I suddenly found that the uneven text layout in the Android project is very serious, so I have to find a way to solve it.

After research, we finally found the reason for the confusion caused by textview automatic line feed - the confusion caused by half angle characters and full angle characters! Generally, the numbers, letters and English punctuation we enter are half width, so the space occupation cannot be determined.

They are very different from the occupation of Chinese characters. For this reason, the typesetting of many characters is uneven.

I have found two ways to solve this problem:

1. Fully diagonalize the characters in textview.

That is, all numbers, letters and punctuation points are converted into full angle characters, so that they occupy two bytes with Chinese characters, so as to avoid the problem of typesetting confusion caused by space occupation. The code for half angle to full angle is as follows, just call.

2. Remove special characters or replace all Chinese labels with English labels.

Use regular expressions to filter all special characters, or use replaceall() to replace Chinese labels with English labels. After conversion, the problem of typesetting confusion can be solved.

Screenshot of uneven layout before resolution:

The neat layout after solution is shown as follows:

The above discussion on text alignment and line breaking of Android textview is all the content shared by Xiaobian. I hope it can give you a reference and support more programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>