Android TextView显示文字对齐

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

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

1.自定义AlignTextView继承系统TextView

[java]  view plain  copy
 print ?
  1. import android.content.Context;  
  2. import android.content.res.TypedArray;  
  3. import android.graphics.Canvas;  
  4. import android.graphics.Color;  
  5. import android.graphics.Paint;  
  6. import android.text.TextUtils;  
  7. import android.util.AttributeSet;  
  8. import android.widget.TextView;  
  9.   
  10. import java.util.ArrayList;  
  11.   
  12. /** 
  13.  * Created by crab on 15-3-16. 
  14.  * 解决中文跟数字在一起的是时候textView显示不正确问题 
  15.  */  
  16. public class AlignTextView extends TextView {  
  17.     //行间距  
  18.     private float mLineGap = 0.0f;  
  19.     private Paint mPaint;  
  20.     private ArrayList<String> mTexts = new ArrayList<String>();  
  21.   
  22.     public AlignTextView(Context context) {  
  23.         super(context);  
  24.         init();  
  25.     }  
  26.   
  27.     public AlignTextView(Context context, AttributeSet attrs) {  
  28.         super(context, attrs);  
  29.         TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.AlignTextView);  
  30.         mLineGap = typedArray.getDimension(R.styleable.AlignTextView_lineSpacingExtra, 0.0f);  
  31.         float textSize = typedArray.getDimension(R.styleable. AlignTextView_textSize, 24.0f);  
  32.         int textColor = typedArray.getColor(R.styleable. AlignTextView_textColor, Color.WHITE);  
  33.         // 构建paint对象  
  34.         mPaint = new Paint();  
  35.         mPaint.setAntiAlias(true);  
  36.         mPaint.setColor(textColor);  
  37.         mPaint.setTextSize(textSize);  
  38.   
  39.         typedArray.recycle();  
  40.         init();  
  41.     }  
  42.   
  43.     public AlignTextView(Context context, AttributeSet attrs, int defStyle) {  
  44.         super(context, attrs, defStyle);  
  45.         TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.AlignTextView);  
  46.         mLineGap = typedArray.getDimension(R.styleable.AlignTextView_lineSpacingExtra, 0.0f);  
  47.         float textSize = typedArray.getDimension(R.styleable. AlignTextView_textSize, 24.0f);  
  48.         int textColor = typedArray.getColor(R.styleable. AlignTextView_textColor, Color.WHITE);  
  49.         // 构建paint对象  
  50.         mPaint = new Paint();  
  51.         mPaint.setAntiAlias(true);  
  52.         mPaint.setColor(textColor);  
  53.         mPaint.setTextSize(textSize);  
  54.         typedArray.recycle();  
  55.         init();  
  56.     }  
  57.   
  58.     private void init() {  
  59.         mTexts.clear();  
  60.     }  
  61.   
  62.     @Override  
  63.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
  64.         super.onMeasure(widthMeasureSpec, heightMeasureSpec);  
  65.         int width = MeasureSpec.getSize(widthMeasureSpec);  
  66.         String text = getText().toString();  
  67.         if (!TextUtils.isEmpty(text)) {  
  68.             mTexts.clear();  
  69.             int iStart = 0;  
  70.             int w = 0;  
  71.             int line = 0;  
  72.             Paint paint=mPaint;  
  73.             for (int i = 0; i < text.length(); i++) {  
  74.                 char ch = text.charAt(i);  
  75.                 float[] charWidth = new float[1];  
  76.                 String charStr = String.valueOf(ch);  
  77.                 paint.getTextWidths(charStr, charWidth);  
  78.                 if (ch == '\n') {  
  79.                     String subText = text.substring(iStart, i);  
  80.                     mTexts.add(line, subText);  
  81.                     line++;  
  82.                     iStart = i + 1;  
  83.                     w = 0;  
  84.                 } else {  
  85.                     w += ((int) Math.ceil(charWidth[0]));  
  86.                     if (w > width) {  
  87.                         String subText = text.substring(iStart, i);  
  88.                         mTexts.add(line, subText);  
  89.                         iStart = i;  
  90.                         w = 0;  
  91.                         line++;  
  92.                     } else {  
  93.                         if (i == text.length() - 1) {  
  94.                             String subText = text.substring(iStart, i+1);  
  95.                             mTexts.add(line, subText);  
  96.                         }  
  97.                     }  
  98.                 }  
  99.             }  
  100.             Paint.FontMetrics fontMetrics=paint.getFontMetrics();  
  101.             float fontHeight=fontMetrics.bottom-fontMetrics.top;  
  102.             int size=mTexts.size();  
  103.             float textHeight=0.0f;  
  104.             for(int i=0;i<size;i++){  
  105.                 if(i==0){  
  106.                     textHeight+=fontHeight;  
  107.                 }else{  
  108.                     textHeight+=(fontHeight+mLineGap);  
  109.                 }  
  110.             }  
  111.             setMeasuredDimension(width,(int)textHeight);  
  112.         }else{  
  113.             mTexts.clear();  
  114.         }  
  115.     }  
  116.   
  117.     @Override  
  118.     protected void onDraw(Canvas canvas) {  
  119.         int size=mTexts.size();  
  120.         Paint paint=mPaint;  
  121.         Paint.FontMetrics fontMetrics=paint.getFontMetrics();  
  122.         float fontHeight=fontMetrics.bottom-fontMetrics.top;  
  123.         for(int i=0;i<size;i++){  
  124.             String text=mTexts.get(i);  
  125.             canvas.translate(0,i*(mLineGap+fontHeight));  
  126.             canvas.drawText(text,0.0f,0-fontMetrics.top,paint);  
  127.             canvas.translate(0,-i*(mLineGap+fontHeight));  
  128.         }  
  129.     }  
  130. }  

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


[html]  view plain  copy
 print ?
  1. <declare-styleable name="AlignTextView">  
  2.        <attr name="lineSpacingExtra" format="dimension"/>  
  3.        <attr name="textSize" format="dimension"/>  
  4.        <attr name="textColor" format="reference|color"/>  
  5.    </declare-styleable>  

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

[html]  view plain  copy
 print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     xmlns:algnText="http://schemas.android.com/apk/res-auto"  
  5.     android:background="#FFFFFF"  
  6.     android:orientation="vertical"  
  7.     android:layout_width="match_parent"  
  8.     android:layout_height="match_parent">  
  9.   
  10.     <TextView  
  11.         android:text="1.系统TextView显示文字效果如下:"  
  12.         android:layout_width="match_parent"  
  13.         android:layout_height="wrap_content"/>  
  14.     <TextView  
  15.         android:layout_marginTop="10dp"  
  16.         android:layout_width="match_parent"  
  17.         android:layout_height="wrap_content"  
  18.         android:lineSpacingExtra="5dp"  
  19.         android:textSize="16sp"  
  20.         android:textColor="#FF00FF00"  
  21.         android:text="我们已将验证码短信发送到你的手机123****4567,超过60秒没收到请点击重发"  
  22.         />  
  23.     <TextView  
  24.         android:layout_marginTop="10dp"  
  25.         android:text="2.自定义TextView显示文字效果如下:"  
  26.         android:layout_width="match_parent"  
  27.         android:layout_height="wrap_content"/>  
  28.     <com.example.crab.mycameratest.AlignTextView  
  29.         android:layout_marginTop="10dp"  
  30.         android:text="我们已将验证码短信发送到你的手机123****4567,超过60秒没收到请点击重发"  
  31.         algnText:lineSpacingExtra="5dp"  
  32.         algnText:textSize="16sp"  
  33.         algnText:textColor="#FF00FF00"  
  34.         android:layout_width="match_parent"  
  35.         android:layout_height="wrap_content"/>  
  36.       
  37. </LinearLayout>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值