The TextView controls in Android are left-aligned by default and are not justified. The possible reason is that the Android default number, the letter can not be the first line after the beginning of each line of characters, because the number, the letter is half-width characters, there is the text of the English characters occupy 1 bytes, and a Chinese character occupies two bytes. Here I introduce the principle of two-pronged alignment:
1, measuring the width of a Chinese character occupied
2. The total number of rows is calculated according to the width of the TextView and the width of a Chinese character and the spacing between characters.
3. Calculate the total height of textview according to padding and margin and line height.
4. Draw each character of each line
The effect is as follows:
The specific code is as follows:
PackageCom.wedroid.framework.module.ui;ImportAndroid.content.Context;ImportAndroid.graphics.Canvas;ImportAndroid.graphics.Paint;ImportAndroid.text.TextPaint;ImportAndroid.text.TextUtils;ImportAndroid.util.AttributeSet;ImportAndroid.view.ViewGroup.MarginLayoutParams;ImportAndroid.view.ViewTreeObserver.OnPreDrawListener;ImportAndroid.widget.TextView; Public class wedroidaligntextview extends TextView { Private BooleanFirst =true; Public Wedroidaligntextview(context context, AttributeSet attrs,intDefstyle) {Super(Context, attrs, Defstyle); Getviewtreeobserver (). Addonpredrawlistener (NewOnpredrawlistener () {@Override Public Boolean Onpredraw() {inittextinfo ();return true; } }); } Public Wedroidaligntextview(context context, AttributeSet attrs) { This(Context, Attrs,0); } Public Wedroidaligntextview(Context context) { This(Context,NULL,0); }Private floatTextSize;Private floatTextlineheight;Private intTopPrivate intYPrivate intLinesPrivate intBottomPrivate intRightPrivate intLeftPrivate intLinedrawwords;Private Char[] Textchararray;Private floatSinglewordwidth;Private floatLinespacingextra; Public void Inittextinfo() {textSize = GetTextSize (); Textlineheight = Getlineheight (); left =0; right = GetRight (); y = GetTop ();//width to draw intDrawtotalwidth = Right-left; String text = GetText (). toString ();if(! Textutils.isempty (text) && first) {Textchararray = Text.tochararray (); Textpaint Mtextpaint =NewTextpaint (Paint.anti_alias_flag); Mtextpaint.density = Getresources (). Getdisplaymetrics (). density; Mtextpaint.settextsize (textSize);//width of a wordSinglewordwidth = Mtextpaint.measuretext ("One") + Linespacingextra;How many characters can be placed on a line//Linedrawwords = (int) (Drawtotalwidth/singlewordwidth);intlength = Textchararray.length; lines = Length/linedrawwords;if(length% linedrawwords) >0) {lines = lines +1; } first =false; Marginlayoutparams layoutparams = (marginlayoutparams) getlayoutparams ();intTotalheight = (int) (lines*textlineheight+textlineheight*2+ Getpaddingbottom () +getpaddingtop () +layoutparams.bottommargin+layoutparams.topmargin); SetHeight (Totalheight); } }@Override protected void OnDraw(Canvas canvas) {bottom = Getbottom ();intDrawtotalline = lines;if(maxline!=0&&drawtotalline>maxline) {drawtotalline = MaxLine; } for(inti =0; i < Drawtotalline; i++) {Try{intlength = Textchararray.length;intMleft = left;//Line i+1 start character index intStartIndex = (I *1) * Linedrawwords;//I+1 line end character Index intEndtextindex = StartIndex + linedrawwords;if(Endtextindex > Length) {endtextindex = length; Y + = Textlineheight; }Else{y + = Textlineheight; } for(; startIndex < endtextindex; startindex++) {Charc = Textchararray[startindex];//if (c = = ") {//c = ' \u3000 ';//} else if (C < ' \177 ') {//C = (char) (c + 65248);// }Canvas.drawtext (String.valueof (c), Mleft, Y, Getpaint ()); Mleft + = Singlewordwidth; } }Catch(Exception e) {E.printstacktrace (); } } }intMaxLine; Public void Setmaxlines(intMax) { This. maxLine = max; } Public void Setlinespacingextra(intLinespacingextra) { This. Linespacingextra = Linespacingextra; }/** * To determine if it is Chinese * @return * * Public Static Boolean Containchinese(String string) {BooleanFlag =false; for(inti =0; I < string.length (); i++) {Charc = String.charat (i);if(c >=0x4e00) && (c <=0x9fa5)) {flag =true; } }returnFlag } Public StaticStringTodbc(String input) {//Causes TextView exception wrapping: Android default numbers, letters cannot be the beginning of each line after the first line, because numbers, letters are half-width characters //So we just need to convert half-width characters to full-width characters CharC[] = Input.tochararray (); for(inti =0; i < c.length; i++) {if(C[i] = ="') {C[i] =' \u3000 '; }Else if(C[i] <' \177 ') {C[i] = (Char) (C[i] +65248); } }return NewString (c); }}
Android TextView justify on both sides