Android TextView顯示文字對齊
有時候利用android的TextView顯示中文跟數位組合會對不齊,如下面,文字還沒有到達螢幕右邊就開始換行了
為瞭解決這個文字,自己子定義了一個TextView的子類來實現,具體步驟如下:
1.自訂AlignTextView繼承系統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. * 解決中文跟數字在一起的是時候textView顯示不正確問題 */public class AlignTextView extends TextView { //行間距 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_textSize, 24.0f); int textColor = typedArray.getColor(R.styleable. AlignTextView_textColor, Color.WHITE); // 構建paint對象 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.obtainStyledAttributes(attrs, R.styleable.AlignTextView); mLineGap = typedArray.getDimension(R.styleable.AlignTextView_lineSpacingExtra, 0.0f); float textSize = typedArray.getDimension(R.styleable. AlignTextView_textSize, 24.0f); int textColor = typedArray.getColor(R.styleable. AlignTextView_textColor, Color.WHITE); // 構建paint對象 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.bottom-fontMetrics.top; int size=mTexts.size(); float textHeight=0.0f; for(int i=0;i2.res/values/attrs.xml檔案中增加如下屬性
3.在布局檔案中使用自訂的View