您的位置:首页 > 移动开发 > Android开发

Android TextView显示文字对齐

2017-01-08 14:25 531 查看
有时候利用Android的TextView显示中文跟数字的组合会对不齐,如下面截图,文字还没有到达屏幕右边就开始换行了



为了解决这个问题,自己自定义了一个TextView的子类来实现,具体步骤如下:

1.自定义AlignTextView继承系统TextView

[java] view
plain copy

 print?

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<String> mTexts = new ArrayList<String>();  

  

    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;i<size;i++){  

                if(i==0){  

                    textHeight+=fontHeight;  

                }else{  

                    textHeight+=(fontHeight+mLineGap);  

                }  

            }  

            setMeasuredDimension(width,(int)textHeight);  

        }else{  

            mTexts.clear();  

        }  

    }  

  

    @Override  

    protected void onDraw(Canvas canvas) {  

        int size=mTexts.size();  

        Paint paint=mPaint;  

        Paint.FontMetrics fontMetrics=paint.getFontMetrics();  

        float fontHeight=fontMetrics.bottom-fontMetrics.top;  

        for(int i=0;i<size;i++){  

            String text=mTexts.get(i);  

            canvas.translate(0,i*(mLineGap+fontHeight));  

            canvas.drawText(text,0.0f,0-fontMetrics.top,paint);  

            canvas.translate(0,-i*(mLineGap+fontHeight));  

        }  

    }  

}  

2.res/values/attrs.xml文件中增加如下属性

[html] view
plain copy

 print?

<declare-styleable name="AlignTextView">  

       <attr name="lineSpacingExtra" format="dimension"/>  

       <attr name="textSize" format="dimension"/>  

       <attr name="textColor" format="reference|color"/>  

   </declare-styleable>  

3.在布局文件中使用自定义的View

[html] view
plain copy

 print?

<?xml version="1.0" encoding="utf-8"?>  

  

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  

    xmlns:algnText="http://schemas.android.com/apk/res-auto"  

    android:background="#FFFFFF"  

    android:orientation="vertical"  

    android:layout_width="match_parent"  

    android:layout_height="match_parent">  

  

    <TextView  

        android:text="1.系统TextView显示文字效果如下:"  

        android:layout_width="match_parent"  

        android:layout_height="wrap_content"/>  

    <TextView  

        android:layout_marginTop="10dp"  

        android:layout_width="match_parent"  

        android:layout_height="wrap_content"  

        android:lineSpacingExtra="5dp"  

        android:textSize="16sp"  

        android:textColor="#FF00FF00"  

        android:text="我们已将验证码短信发送到你的手机123****4567,超过60秒没收到请点击重发"  

        />  

    <TextView  

        android:layout_marginTop="10dp"  

        android:text="2.自定义TextView显示文字效果如下:"  

        android:layout_width="match_parent"  

        android:layout_height="wrap_content"/>  

    <com.example.crab.mycameratest.AlignTextView  

        android:layout_marginTop="10dp"  

        android:text="我们已将验证码短信发送到你的手机123****4567,超过60秒没收到请点击重发"  

        algnText:lineSpacingExtra="5dp"  

        algnText:textSize="16sp"  

        algnText:textColor="#FF00FF00"  

        android:layout_width="match_parent"  

        android:layout_height="wrap_content"/>  

      

</LinearLayout> 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: