By customizing the TextView control----
public class Justifytextview extends TextView {private int mliney;private int mviewwidth;public static final String two_ch Inese_blank = "";p ublic Justifytextview (context context, AttributeSet Attrs) {Super (context, attrs);} @Overrideprotected void OnLayout (Boolean changed, int left, int top, int. Right,int bottom) {super.onlayout (changed, left, Top, right, bottom);} @Overrideprotected void OnDraw (canvas canvas) {Textpaint paint = getpaint ();p Aint.setcolor (Getcurrenttextcolor ()); Paint.drawablestate = Getdrawablestate (); mviewwidth = Getmeasuredwidth (); String text = GetText (). toString (); Mliney = 0;mliney + = GetTextSize (); Layout layout = GetLayout ();//Layout.getlayout () appears in 4.4.3 nullpointerexceptionif (layout = = null) {return;} Paint.fontmetrics fm = paint.getfontmetrics (), int textHeight = (int) (Math.ceil (fm.descent-fm.ascent)); textHeight = (in T) (TextHeight * layout.getspacingmultiplier () + Layout.getspacingadd ());//Resolves a problem where the last line of text is too large for (int i = 0; i < LAYOUT.G Etlinecount (); i++) {int LInestart = Layout.getlinestart (i); int lineend = Layout.getlineend (i); float width = staticlayout.getdesiredwidth (text, Linestart,lineend, Getpaint ()); String line = text.substring (Linestart, Lineend), if (I < Layout.getlinecount ()-1) {if (Needscale (line)) {Drawscaledte XT (Canvas, Linestart, line, width);} else {canvas.drawtext (line, 0, Mliney, paint);}} else {canvas.drawtext (line, 0, Mliney, paint);} Mliney + = TextHeight;}} private void Drawscaledtext (canvas canvas, int linestart, String line,float linewidth) {float x = 0;if (Isfirstlineofparag Raph (Linestart, line)) {String blanks = ""; Canvas.drawtext (Blanks, X, Mliney, Getpaint ()); float bw = Staticlayout.getdes Iredwidth (Blanks, getpaint ()); x + = Bw;line = line.substring (3);} int gapcount = Line.length ()-1;int i = 0;if (Line.length () > 2 && line.charat (0) = = 12288&& Line.char at (1) = = 12288) {String substring = line.substring (0, 2); float CW = staticlayout.getdesiredwidth (substring, getpaint ()); c Anvas.drawtext (Substring, X, Mliney, Getpaint ()); x + = Cw;i + = 2;} Float d = (mviewwidth-linewidth)/gapcount;for (; i < line.length (); i++) {String c = string.valueof (Line.charat (i)) ; float CW = Staticlayout.getdesiredwidth (c, Getpaint ()); Canvas.drawtext (c, X, Mliney, Getpaint ()); x + = CW + D;}} Private Boolean isfirstlineofparagraph (int linestart, String line) {return line.length () > 3 && line.charat (0) = = ' && line.charat (1) = = ';} Private Boolean Needscale (String line) {if (line = = NULL | | line.length () = = 0) {return false;} else {return Line.charat (l Ine.length ()-1)! = ' \ n ';}}
--Solve the problem of TextView-line typesetting confusion--find a good way online--share to everyone