JustifyTextView 解决TextView中英文混排排版问题,android文字排版不齐,

   最近在做着一个项目,里边会显示很长的一段文字,但是这些文字并不会整齐地排列,遇到文字中带有中英文时,果断给我换行了,好无语..接着就是不断地百度百度,找到了一堆一两年前的东西,不是叫你半角转全角,就是中文符号转英文符号,还有一些自定义TextView的试过了效果也不满意,最后在一个Android开发群中才得知JustifyTextView

        GitHub地址:https://github.com/ufo22940268/android-justifiedtextview

借用一下原图:

英文:


中文:



最后是自定义TextView的代码(从GitHup中下载的JustifyTextView有一个小Bug,最后一行显示的文字间距太大,在Issues(https://github.com/ufo22940268/android-justifiedtextview/issues/1)中作者也告知了如何解决,只是Githup代码中未更新,这里的代码已做出了修改):

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. import android.content.Context;  
  2. import android.graphics.Canvas;  
  3. import android.graphics.Paint;  
  4. import android.text.Layout;  
  5. import android.text.StaticLayout;  
  6. import android.text.TextPaint;  
  7. import android.util.AttributeSet;  
  8. import android.widget.TextView;  
  9.   
  10. /** 
  11.  * @author ccheng 
  12.  * @Date 3/18/14 
  13.  */  
  14. public class JustifyTextView extends TextView {  
  15.   
  16.     private int mLineY;  
  17.     private int mViewWidth;  
  18.     public static final String TWO_CHINESE_BLANK = "  ";  
  19.   
  20.     public JustifyTextView(Context context, AttributeSet attrs) {  
  21.         super(context, attrs);  
  22.     }  
  23.   
  24.     @Override  
  25.     protected void onLayout(boolean changed, int left, int top, int right,  
  26.             int bottom) {  
  27.         super.onLayout(changed, left, top, right, bottom);  
  28.     }  
  29.   
  30.     @Override  
  31.     protected void onDraw(Canvas canvas) {  
  32.         TextPaint paint = getPaint();  
  33.         paint.setColor(getCurrentTextColor());  
  34.         paint.drawableState = getDrawableState();  
  35.         mViewWidth = getMeasuredWidth();  
  36.         String text = getText().toString();  
  37.         mLineY = 0;  
  38.         mLineY += getTextSize();  
  39.         Layout layout = getLayout();  
  40.   
  41.         // layout.getLayout()在4.4.3出现NullPointerException  
  42.         if (layout == null) {  
  43.             return;  
  44.         }  
  45.   
  46.         Paint.FontMetrics fm = paint.getFontMetrics();  
  47.   
  48.         int textHeight = (int) (Math.ceil(fm.descent - fm.ascent));  
  49.         textHeight = (int) (textHeight * layout.getSpacingMultiplier() + layout  
  50.                 .getSpacingAdd());  
  51.         //解决了最后一行文字间距过大的问题  
  52.         for (int i = 0; i < layout.getLineCount(); i++) {  
  53.             int lineStart = layout.getLineStart(i);  
  54.             int lineEnd = layout.getLineEnd(i);  
  55.             float width = StaticLayout.getDesiredWidth(text, lineStart,  
  56.                     lineEnd, getPaint());  
  57.             String line = text.substring(lineStart, lineEnd);  
  58.               
  59.             if(i < layout.getLineCount() - 1) {  
  60.                 if (needScale(line)) {  
  61.                     drawScaledText(canvas, lineStart, line, width);  
  62.                 } else {  
  63.                     canvas.drawText(line, 0, mLineY, paint);  
  64.                 }  
  65.             } else {  
  66.                 canvas.drawText(line, 0, mLineY, paint);  
  67.             }  
  68.             mLineY += textHeight;  
  69.         }  
  70.     }  
  71.   
  72.     private void drawScaledText(Canvas canvas, int lineStart, String line,  
  73.             float lineWidth) {  
  74.         float x = 0;  
  75.         if (isFirstLineOfParagraph(lineStart, line)) {  
  76.             String blanks = "  ";  
  77.             canvas.drawText(blanks, x, mLineY, getPaint());  
  78.             float bw = StaticLayout.getDesiredWidth(blanks, getPaint());  
  79.             x += bw;  
  80.   
  81.             line = line.substring(3);  
  82.         }  
  83.   
  84.         int gapCount = line.length() - 1;  
  85.         int i = 0;  
  86.         if (line.length() > 2 && line.charAt(0) == 12288  
  87.                 && line.charAt(1) == 12288) {  
  88.             String substring = line.substring(02);  
  89.             float cw = StaticLayout.getDesiredWidth(substring, getPaint());  
  90.             canvas.drawText(substring, x, mLineY, getPaint());  
  91.             x += cw;  
  92.             i += 2;  
  93.         }  
  94.   
  95.         float d = (mViewWidth - lineWidth) / gapCount;  
  96.         for (; i < line.length(); i++) {  
  97.             String c = String.valueOf(line.charAt(i));  
  98.             float cw = StaticLayout.getDesiredWidth(c, getPaint());  
  99.             canvas.drawText(c, x, mLineY, getPaint());  
  100.             x += cw + d;  
  101.         }  
  102.     }  
  103.   
  104.     private boolean isFirstLineOfParagraph(int lineStart, String line) {  
  105.         return line.length() > 3 && line.charAt(0) == ' '  
  106.                 && line.charAt(1) == ' ';  
  107.     }  
  108.   
  109.     private boolean needScale(String line) {  
  110.         if (line == null || line.length() == 0) {  
  111.             return false;  
  112.         } else {  
  113.             return line.charAt(line.length() - 1) != '\n';  
  114.         }  
  115.     }  
  116.   
  117. }  

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值