Android TextView display text alignment
Sometimes the combination of Chinese and numbers is displayed in TextView of android. For example, if the text below does not reach the right of the screen, a new line is started.
To solve this problem, define a subclass of TextView as follows:
1. Custom AlignTextView inherits the system TextView
Import android. content. context; import android. content. res. typedArray; import android. graphics. canvas; import android. graphics. color; import android. graphics. paint; import android. text. textUtils; import android. util. attributeSet; import android. widget. textView; import java. util. arrayList;/*** Created by crab on 15-3-16. * solve the problem of incorrect textView display when Chinese and numbers are together */public class AlignTextView extends TextView {// line spacing private float mLineGap = 0.0f; private Paint mPaint; private ArrayList
MTexts = new ArrayList
(); Public AlignTextView (Context context) {super (context); init ();} public AlignTextView (Context context, AttributeSet attrs) {super (context, attrs ); typedArray typedArray = context. obtainStyledAttributes (attrs, R. styleable. alignTextView); mLineGap = typedArray. getDimension (R. styleable. alignTextView_lineSpacingExtra, 0.0f); float textSize = typedArray. getDimension (R. styleable. alignTextView_text Size, 24366f); int textColor = typedArray. getColor (R. styleable. alignTextView_textColor, Color. WHITE); // build the paint object mPaint = new Paint (); mPaint. setAntiAlias (true); mPaint. setColor (textColor); mPaint. setTextSize (textSize); typedArray. recycle (); init ();} public AlignTextView (Context context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle); TypedArray typedArray = context. obtainS TyledAttributes (attrs, R. styleable. alignTextView); mLineGap = typedArray. getDimension (R. styleable. alignTextView_lineSpacingExtra, 0.0f); float textSize = typedArray. getDimension (R. styleable. alignTextView_textSize, 24366f); int textColor = typedArray. getColor (R. styleable. alignTextView_textColor, Color. WHITE); // build the paint object mPaint = new Paint (); mPaint. setAntiAlias (true); mPaint. setColor (textColor ); MPaint. setTextSize (textSize); typedArray. recycle (); init ();} private void init () {mTexts. clear () ;}@ Override protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {super. onMeasure (widthMeasureSpec, heightMeasureSpec); int width = MeasureSpec. getSize (widthMeasureSpec); String text = getText (). toString (); if (! TextUtils. isEmpty (text) {mTexts. clear (); int iStart = 0; int w = 0; int line = 0; Paint paint = mPaint; for (int I = 0; I <text. length (); I ++) {char ch = text. charAt (I); float [] charWidth = new float [1]; String charStr = String. valueOf (ch); paint. getTextWidths (charStr, charWidth); if (ch = '\ n') {String subText = text. substring (iStart, I); mTexts. add (line, subText); line ++; iStart = I + 1; w = 0;} else {w + = (int) Math. ceil (charWidth [0]); if (w> width) {String subText = text. substring (iStart, I); mTexts. add (line, subText); iStart = I; w = 0; line ++;} else {if (I = text. length ()-1) {String subText = text. substring (iStart, I + 1); mTexts. add (line, subText) ;}}} Paint. fontMetrics fontMetrics = paint. getFontMetrics (); float fontHeight = fontMetrics. the bottom-fontMetrics.top; int size = mTexts. size (); float textHeight = 0.0f; for (int I = 0; I
2. Add the following attributes to the res/values/attrs. xml file:
3. Use custom View in the layout File