Android TextView sets the font spacing and line spacing. androidtextview

Source: Internet
Author: User

Android TextView sets the font spacing and line spacing. androidtextview

1. If you want to set the row spacing of TextView, use it (no more than 2)

android:lineSpacingMultiplier="1.8"
2. If you want to set the padding, you need a custom control.
<Pre name = "code" class = "java"> import android. content. context; import android. graphics. *; import android. text. textUtils; import android. util. attributeSet; import android. widget. textView; import java. util. regex. matcher; import java. util. regex. pattern;/*** Created by mrni-mac on 14-11-25. */public class MyTextView extends TextView {private String content; private int width; private Paint paint; private in T xPadding; private int yPadding; private int textHeight; private int xPaddingMin; int count; // records the two-dimensional array int [] [] position of each word; public MyTextView (Context context) {super (context); init ();} public MyTextView (Context context, AttributeSet attrs) {super (context, attrs); init ();} public MyTextView (Context context, attributeSet attrs, int defStyle) {super (context, attrs, defStyle); init ();} public v Oid setText (String str) {width = this. getWidth (); getPositions (str); // redraw the control this. invalidate ();} public void init () {paint = new Paint (); paint. setColor (Color. parseColor ("#888888"); paint. setTypeface (Typeface. DEFAULT); paint. setTextSize (dip2px (this. getContext (), 14f); Paint. fontMetrics fm = paint. getFontMetrics (); // obtain the default font attribute textHeight = (int) (Math. ceil (fm. descent-fm. top) + 2); // obtain the font height // Padding xPadding = dip2px (this. getContext (), 4f); // The row spacing yPadding = dip2px (this. getContext (), 10f); // relatively small padding (compact letters and numbers) xPaddingMin = dip2px (this. getContext (), 2f) ;}@ Override protected void onDraw (Canvas canvas) {super. onDraw (canvas); if (! TextUtils. isEmpty (content) {for (int I = 0; I <count; I ++) {canvas. drawText (String. valueOf (content. charAt (I), position [I] [0], position [I] [1], paint) ;}} public void getPositions (String content) {this. content = content; char ch; // The x coordinate int x of the Input Point = 0; // the current number of rows int lineNum = 1; count = content. length (); // initialize the font position array position = new int [count] [2]; for (int I = 0; I <count; I ++) {ch = content. charAt (I); String str = String. valueOf (ch); // The rect that is displayed for each character based on the paint brush is the wrap-around box (get the character width) Rect rect = new Rect (); paint. getTextBounds (str, 0, 1, rect); int strwidth = rect. width (); // process some punctuations if (str. equals ("") | str. equals ("(") {strwidth + = xPaddingMin * 2;} // float textWith = strwidth of the current row; // pre-determine whether textWith exists before you draw the text; // if (x> width) {lineNum ++ is used to wrap the line if (x> width; // The actual number of rows plus one x = 0;} else {// return to the pre-prediction position x-= textWith ;} // record the position of each word [I] [0] = x; position [I] [1] = textHeight * lineNum + yPadding * (lineNum-1 ); // determine whether it is a number or a letter (numbers and letters should be compact) // enter the next input position after each input. if (isNumOrLetters (str )) {x + = textWith + xPaddingMin;} else {x + = textWith + xPadding ;}// set the height of the Control Based on the Content drawn. this. setHeight (textHeight + yPadding) * lineNum);} // tool class: determines whether it is a letter or number public boolean isNumOrLetters (String str) {String regEx = "^ [A-Za-z0-9 _] + $"; Pattern p = Pattern. compile (regEx); Matcher m = p. matcher (str); return m. matches ();} // tool class: Use the dp Method in the Code (because the number in the Code is directly used to represent the pixel) public static int dip2px (Context context, float dip) {final float scale = context. getResources (). getDisplayMetrics (). density; return (int) (dip * scale + 0.5f );}}


Use xml as follows 

<Current package name. MyTextView android: layout_width = "match_parent" android: layout_height = "wrap_content" android: id = "@ + id/video_dec"/>
The effect is as follows:




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.