1. Use if you want to set the line spacing of the TextView (do not look at the 2)
Android:linespacingmultiplier= "1.8"
2. If you want to set the word spacing, 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 int xpadding; private int ypadding; private int textHeight; private int xpaddingmin; int count; Record the two-dimensional array of each word int[][] position; 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 void SetText (String str) {width = This.getwidth (); Getpositions (str); Redraw controls 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 ();//Get system default font properties TextHeight = (int) (Math.ceil (fm.descent-fm.top) + 2) ///Get font height//word spacing xpadding = dip2px (This.getcontext (), 4f); Line spacing ypadding = dip2px (This.getcontext (), 10f); Relatively small word spacing (letters and numbers should be compact) 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.valueo F (Content.charat (i)), position[i][0],position[i][1], paint); }}} public void Getpositions (String content) {this.content = content; Char ch; The x of the input pointcoordinate int x = 0; Current number of rows int linenum = 1; Count = Content.length (); Initializes an array of font positions position=new int[count][2]; for (int i = 0; i < count; i++) {ch =content.charat (i); String str = string.valueof (CH); The rect that gets the display of each character according to the brush is the bounding box (gets the character width) of the rect rect = new rect (); Paint.gettextbounds (str, 0, 1, rect); int strwidth = Rect.width (); Do some punctuation if (str.equals ("") | | | str.equals ("(")) {strwidth + = Xpaddingmin * 2; }//When the width of the forward float textwith = strwidth; No stooped pre-judgment to see if it will be out of bounds x + = Textwith; Line break if (x > Width) {linenum++;//true number of rows plus one x = 0; } else {//return to pre-contract position X-= Textwith; }//Record the position of each word position[i][0]=x; Position[i][1]=textheight * linenum + ypadding * (linenum-1); Determine if the numbers or letters (numbers and letters should be compact)//Enter the next input position each time the input is ready if (Isnumorletters (str)) {x + = Tex Twith + xpaddingmin; } else {x + = Textwith + xpadding; }}//Set the control's height this.setheight ((textHeight +ypadding) * LineNum) based on the content being drawn; }//Tool class: Determines whether it is a letter or a numeric 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: Method of using DP in code (because pixels are directly represented in code) public static int dip2px (context context, float dip) {Final float SC Ale = Context.getresources (). Getdisplaymetrics (). density; return (int) (DIP * scale + 0.5f); }}
The XML uses the following
< current package name. Mytextview android:layout_width= "match_parent" android:layout_height= "wrap_content" android:id= "@+ Id/video_dec " />
The effect is as follows
Android TextView set word spacing and line spacing