Android TextView顯示文字對齊

來源:互聯網
上載者:User

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
                    



 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.