温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android TextView两端对齐解决办法

发布时间:2020-10-08 05:56:05 来源:脚本之家 阅读:566 作者:lqh 栏目:移动开发

Android TextView两端对齐解决办法

今天遇到一个关于TextView文字两端对齐其实方案,大家都知道原生控件是不能满足我们的需求的,因此需要自定义View

下面看下效果图

Android TextView两端对齐解决办法

package com.example.VerticalMarqueeTextView.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.text.TextPaint;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.TextView;

/**
 * Created by John on 2017/2/9.
 */

public class WordAlignTextView extends TextView {
  private float textSize;
  private float textLineHeight;
  //顶部
  private int top;
  //y轴
  private int y;
  //线
  private int lines;
  //底部
  private int bottom;
  //右边
  private int right;
  //左边
  private int left;
  //线字
  private int lineDrawWords;
  private char[] textCharArray;
  private float singleWordWidth;
   //每个字符的空隙
  private float lineSpacingExtra;

  private boolean isFirst = true;

  public WordAlignTextView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
      @Override
      public boolean onPreDraw() {
        initTextInfo();
        return true;
      }
    });
  }

  public WordAlignTextView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
  }

  public WordAlignTextView(Context context) {
    this(context, null, 0);
  }


  public void initTextInfo() {
    textSize = getTextSize();
    //获取线的高度
    textLineHeight = getLineHeight();
    left = 0;
    right = getRight();
    y = getTop();
    // 要画的宽度
    int drawTotalWidth = right - left;
    String text = getText().toString();
    if (!TextUtils.isEmpty(text) && isFirst) {
      textCharArray = text.toCharArray();
      TextPaint mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
      mTextPaint.density = getResources().getDisplayMetrics().density;
      mTextPaint.setTextSize(textSize);
      // 获取单个单词的的宽度
      singleWordWidth = mTextPaint.measureText("一") + lineSpacingExtra;
      // 每行可以放多少个字符
      lineDrawWords = (int) (drawTotalWidth / singleWordWidth);
      int length = textCharArray.length;
      lines = length / lineDrawWords;
      if ((length % lineDrawWords) > 0) {
        lines = lines + 1;
      }
      first = false;
      ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) getLayoutParams();
      int totalHeight = (int) (lines*textLineHeight+textLineHeight*2 + getPaddingBottom()+getPaddingTop()+layoutParams.bottomMargin+layoutParams.topMargin);
      setHeight(totalHeight);
    }
  }

  @Override
  protected void onDraw(Canvas canvas) {
    bottom = getBottom();
    int drawTotalLine = lines;

    if(maxLine!=0&&drawTotalLine>maxLine){
      drawTotalLine = maxLine;
    }

    for (int i = 0; i < drawTotalLine; i++) {
      try {
        int length = textCharArray.length;
        int mLeft = left;
        // 第i+1行开始的字符index
        int startIndex = (i * 1) * lineDrawWords;
        // 第i+1行结束的字符index
        int endTextIndex = startIndex + lineDrawWords;
        if (endTextIndex > length) {
          endTextIndex = length;
          y += textLineHeight;
        } else {
          y += textLineHeight;
        }
        for (; startIndex < endTextIndex; startIndex++) {
          char c = textCharArray[startIndex];
//     if (c == ' ') {
//      c = '\u3000';
//     } else if (c < '\177') {
//      c = (char) (c + 65248);
//     }
          canvas.drawText(String.valueOf(c), mLeft, y, getPaint());
          mLeft += singleWordWidth;
        }
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }

  int maxLine;

  public void setMaxLines(int max){
    this.maxLine = max;
  }

  public void setLineSpacingExtra(int lineSpacingExtra){
    this.lineSpacingExtra = lineSpacingExtra;
  }

  /**
   * 判断是否为中文
   * @return
   */
  public static boolean containChinese(String string){
    boolean flag = false;
    for (int i = 0; i < string.length(); i++) {
      char c = string.charAt(i);
      if ((c >= 0x4e00) && (c <= 0x9FA5)) {
        flag = true;
      }
    }
    return flag;
  }

  public static String ToDBC(String input) {
    // 导致TextView异常换行的原因:安卓默认数字、字母不能为第一行以后每行的开头字符,因为数字、字母为半角字符
    // 所以我们只需要将半角字符转换为全角字符即可
    char c[] = input.toCharArray();
    for (int i = 0; i < c.length; i++) {
      if (c[i] == ' ') {
        c[i] = '\u3000';
      } else if (c[i] < '\177') {
        c[i] = (char) (c[i] + 65248);
      }
    }
    return new String(c);
  }

}

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

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI