AndroID TextVIEw两端对齐解决办法
今天遇到一个关于TextVIEw文字两端对齐其实方案,大家都知道原生控件是不能满足我们的需求的,因此需要自定义view
下面看下效果图
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,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 < '7') {// 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] < '7') { c[i] = (char) (c[i] + 65248); } } return new String(c); }}
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
总结以上是内存溢出为你收集整理的Android TextView两端对齐解决办法全部内容,希望文章能够帮你解决Android TextView两端对齐解决办法所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)