Android TextView两端对齐解决办法
程序员文章站
2022-06-29 11:57:03
android textview两端对齐解决办法
今天遇到一个关于textview文字两端对齐其实方案,大家都知道原生控件是不能满足我们的需求的,因此需要自定义view...
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, 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); } }
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
上一篇: Skyline AI获1800万美元A轮融资 提供房产投资决策
下一篇: 网店转让的形成与优势