--To solve the problem of TextView-line layout Confusion--Find a good method on the Internet--share to everyone

Source: Internet
Author: User
Tags drawtext

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

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.