欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android 解决TextView排版参差不齐的问题

程序员文章站 2023-12-21 11:40:16
 android 解决textview排版参差不齐的问题 在app中,展示数据时,里面有汉字、数字、特殊字符时,由于全角、半角问题导致textview参差不齐。...

 android 解决textview排版参差不齐的问题

在app中,展示数据时,里面有汉字、数字、特殊字符时,由于全角、半角问题导致textview参差不齐。在网上找了许多,半角转全角并没什么用,还有其他自定义textview都有问题。最后终于找到一个,就像word一样,可以使文字左右两端对齐:

package com.monkey.monkeymushroom.view;

import android.content.context;
import android.graphics.canvas;
import android.graphics.paint;
import android.text.layout;
import android.text.staticlayout;
import android.text.textpaint;
import android.util.attributeset;
import android.widget.textview;

/**
 * 解决文字排版混乱参差不齐的问题
 */
public class justifytextview extends textview {

 private int mliney;
 private int mviewwidth;
 public static final string two_chinese_blank = " ";

 public justifytextview(context context, attributeset attrs) {
  super(context, attrs);
 }

 @override
 protected void onlayout(boolean changed, int left, int top, int right, int bottom) {
  super.onlayout(changed, left, top, right, bottom);
 }

 @override
 protected void ondraw(canvas canvas) {
  textpaint paint = getpaint();
  paint.setcolor(getcurrenttextcolor());
  paint.drawablestate = getdrawablestate();
  mviewwidth = getmeasuredwidth();
  string text = gettext().tostring();
  mliney = 0;
  mliney += gettextsize();
  layout layout = getlayout();

  // layout.getlayout()在4.4.3出现nullpointerexception
  if (layout == null) {
   return;
  }

  paint.fontmetrics fm = paint.getfontmetrics();

  int textheight = (int) (math.ceil(fm.descent - fm.ascent));
  textheight = (int) (textheight * layout.getspacingmultiplier() + layout
    .getspacingadd());
  //解决了最后一行文字间距过大的问题
  for (int i = 0; i < layout.getlinecount(); i++) {
   int linestart = layout.getlinestart(i);
   int lineend = layout.getlineend(i);
   float width = staticlayout.getdesiredwidth(text, linestart,
     lineend, getpaint());
   string line = text.substring(linestart, lineend);

   if (i < layout.getlinecount() - 1) {
    if (needscale(line)) {
     drawscaledtext(canvas, linestart, line, width);
    } else {
     canvas.drawtext(line, 0, mliney, paint);
    }
   } else {
    canvas.drawtext(line, 0, mliney, paint);
   }
   mliney += textheight;
  }
 }

 private void drawscaledtext(canvas canvas, int linestart, string line, float linewidth) {
  float x = 0;
  if (isfirstlineofparagraph(linestart, line)) {
   string blanks = " ";
   canvas.drawtext(blanks, x, mliney, getpaint());
   float bw = staticlayout.getdesiredwidth(blanks, getpaint());
   x += bw;

   line = line.substring(3);
  }

  int gapcount = line.length() - 1;
  int i = 0;
  if (line.length() > 2 && line.charat(0) == 12288
    && line.charat(1) == 12288) {
   string substring = line.substring(0, 2);
   float cw = staticlayout.getdesiredwidth(substring, getpaint());
   canvas.drawtext(substring, x, mliney, getpaint());
   x += cw;
   i += 2;
  }

  float d = (mviewwidth - linewidth) / gapcount;
  for (; i < line.length(); i++) {
   string c = string.valueof(line.charat(i));
   float cw = staticlayout.getdesiredwidth(c, getpaint());
   canvas.drawtext(c, x, mliney, getpaint());
   x += cw + d;
  }
 }

 private boolean isfirstlineofparagraph(int linestart, string line) {
  return line.length() > 3 && line.charat(0) == ' '
    && line.charat(1) == ' ';
 }

 private boolean needscale(string line) {
  if (line == null || line.length() == 0) {
   return false;
  } else {
   return line.charat(line.length() - 1) != '\n';
  }
 }
}

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

上一篇:

下一篇: