[Android] TextView achieves decentralized alignment (alignment at both ends)

Source: Internet
Author: User

TextView is a basic Android control. It is indispensable for text. However, in the recent project development process, I found that TextView has many limitations. The most headache is the TextView text layout problem. We all know that the text Alignment Methods in word are left, right, center, and scattered alignment, but TextView does not have the attribute setting of scattered alignment. This leads to the uneven right side of a text section in TextView. Because the width of each character in Chinese is not very bad, it seems too much in English.

To solve this problem, a common solution is to use html in TextView to set text styles, or simply discard TextView and use WebView. However, we should dare to solve the problem instead of avoiding the problem. I believe that it can be implemented even if only TextView is used. Later, I found that there is an answer on stackoverflow to provide a way of thinking, based on this idea, I realized the scattered alignment of TextView text. The original URL is as follows.

The following is my implementation process:

MainActivity:

Display display = mArticleTextView. setLineSpacing (0f, 1.5f8 * () width/mArticleTextView. setText ("TextView text content to be displayed"

First, set the display font size and text content of TextView. Here, set the font size to be adjusted based on the screen size. Then, call the justify method in the Custom class Textustification to implement the scattered alignment of TextView. The two parameters are the TextView control and the width of the control.

The content of the custom class TextJustification is as follows:

    justify(TextView textView, == ""=<String> paraList =  ArrayList<String>=( i = 0; i<paraList.size(); i++<String> lineList== TextUtils.join(" ", lineList).replaceFirst("\\s*", ""+= tempText.replaceFirst("\\s*", "") + "\n"      ArrayList<String><String> paraList =  ArrayList<String>= text.split("\\n+"      ArrayList<String> lineBreak(String text, Paint paint, =text.split("\\s"<String> lineList =  ArrayList<String>=""(paint.measureText(myText+" "+word)<==myText+" "+ totalSpacesToInsert=()((contentWidth-paint.measureText(myText))/paint.measureText(" "=      String justifyLine(String text,=text.split("\\s"=" "((totalSpacesToInsert)>=(wordArray.length-1=toAppend+" "=totalSpacesToInsert-(wordArray.length-1 i=0=""(i<=justifiedText+word+" "+=justifiedText+word+++

This class completes the text layout in TextView, which consists of three steps:
1. Divide an article into several paragraphs (skip this step if there is only one section );

2. Split the text in each segment into various words, and then determine the maximum number of words that can be filled in each line based on the control length, and fill in a few spaces when calculating that the row is full.

3. Enter a space.

Note that some regular expressions are used in the code to operate the content of the article. If you are not clear, you can search for the meaning by yourself.

 

In this way, the layout process of text dispersion and alignment in TextView is completed. To sum up, this operation is still a bit painful. Although it is not complicated, it still completely reprocesses the text content; in addition, alignment is achieved only by using the number of existing texts and spaces, rather than strictly decentralized alignment. However, after all, TextView operations are implemented, and the results are not bad.

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.