Android重写TextView实现文字整齐排版的方法(附demo源码下载)
程序员文章站
2024-02-20 23:29:58
本文实例讲述了android重写textview实现文字整齐排版的方法。分享给大家供大家参考,具体如下:
xrtextview类
package rong.an...
本文实例讲述了android重写textview实现文字整齐排版的方法。分享给大家供大家参考,具体如下:
xrtextview类
package rong.android.test; import org.json.jsonarray; import org.json.jsonexception; import android.content.context; import android.graphics.canvas; import android.graphics.color; import android.graphics.paint; import android.util.attributeset; import android.view.view; import android.widget.textview; public class xrtextview extends textview{ private final string namespace = "rong.android.textview"; private string text; private float textsize; private float paddingleft; private float paddingright; private float marginleft; private float marginright; private int textcolor; private jsonarray colorindex; private paint paint1 = new paint(); private paint paintcolor = new paint(); private float textshowwidth; private float spacing = 0; private float linespacing = 1.3f;//行与行的间距 public xrtextview(context context, attributeset attrs) { super(context, attrs); text = attrs.getattributevalue( "http://schemas.android.com/apk/res/android", "text"); textsize = attrs.getattributeintvalue(namespace, "textsize", 25);//字体大小 textcolor = attrs.getattributeintvalue(namespace, "textcolor",color.blue);//字体颜色 paddingleft = attrs.getattributeintvalue(namespace, "paddingleft", 0); paddingright = attrs.getattributeintvalue(namespace, "paddingright", 0); marginleft = attrs.getattributeintvalue(namespace, "marginleft", 0); marginright = attrs.getattributeintvalue(namespace, "marginright", 0); paint1.settextsize(textsize); paint1.setcolor(textcolor); paint1.setantialias(true); paintcolor.setantialias(true); paintcolor.settextsize(textsize); paintcolor.setcolor(color.blue); } public xrtextview(context context, float textsize, int textcolor, float paddingleft, float paddingright, float marginleft, float marginright){ super(context); this.textsize = textsize; this.textcolor = textcolor; this.paddingleft = paddingleft; this.paddingright = paddingright; this.marginleft = marginleft; this.marginright = marginright; paint1.settextsize(textsize); paint1.setcolor(textcolor); paint1.setantialias(true); paintcolor.setantialias(true); paintcolor.settextsize(textsize); paintcolor.setcolor(color.blue); } public jsonarray getcolorindex() { return colorindex; } public void setcolorindex(jsonarray colorindex) { this.colorindex = colorindex; } /** * 传入一个索引,判断当前字是否被高亮 * @param index * @return * @throws jsonexception */ public boolean iscolor(int index) throws jsonexception{ if(colorindex == null){ return false; } for(int i = 0 ; i < colorindex.length() ; i ++){ jsonarray array = colorindex.getjsonarray(i); int start = array.getint(0); int end = array.getint(1)-1; if(index >= start && index <= end){ return true; } } return false; } @override protected void ondraw(canvas canvas) { // super.ondraw(canvas); view view=(view)this.getparent(); textshowwidth=view.getmeasuredwidth()-paddingleft - paddingright - marginleft - marginright; int linecount = 0; text = this.gettext().tostring();//.replaceall("\n", "\r\n"); if(text==null)return; char[] textchararray = text.tochararray(); // 已绘的宽度 float drawedwidth = 0; float charwidth; for (int i = 0; i < textchararray.length; i++) { charwidth = paint1.measuretext(textchararray, i, 1); if(textchararray[i]=='\n'){ linecount++; drawedwidth = 0; continue; } if (textshowwidth - drawedwidth < charwidth) { linecount++; drawedwidth = 0; } boolean color = false; try { color = iscolor(i); } catch (jsonexception e1) { // todo auto-generated catch block e1.printstacktrace(); } if(color){ canvas.drawtext(textchararray, i, 1, paddingleft + drawedwidth, (linecount + 1) * textsize * linespacing, paintcolor); }else{ canvas.drawtext(textchararray, i, 1, paddingleft + drawedwidth, (linecount + 1) * textsize * linespacing, paint1); } if(textchararray[i] > 127 && textchararray[i] != '、' && textchararray[i] != ',' && textchararray[i] != '。' && textchararray[i] != ':' && textchararray[i] != '!'){ drawedwidth += charwidth + spacing; }else{ drawedwidth += charwidth; } } setheight((int) ((linecount + 1) * (int) textsize * linespacing + 10)); } public float getspacing() { return spacing; } public void setspacing(float spacing) { spacing = spacing; } public float getmylinespacing() { return linespacing; } public void setmylinespacing(float linespacing) { linespacing = linespacing; } public float getmytextsize() { return textsize; } public void setmytextsize(float textsize) { this.textsize = textsize; paint1.settextsize(textsize); paintcolor.settextsize(textsize); } }
mainactivity类
package rong.android.test; import android.os.bundle; import android.widget.textview; import android.app.activity; public class mainactivity extends activity { private xrtextview xrtextview = null; private textview textview = null; private string content = "abcdefgabcdef我要你lfwjkdfl;skjf asljkflskjfls;kjfsljfwfisdlfjsllkjsdfjlskjf546132s1f3sd4f31s3dffslfksjdfljlsadkjflsajdf sdfjklsajdflsa;jdfls 的!@#$%^&*()_"; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); xrtextview = (xrtextview) this.findviewbyid(r.id.mytextview_tv); xrtextview.settext(content); textview = (textview) this.findviewbyid(r.id.mytextview_tv1); textview.settext(content); } }
布局文件
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <rong.android.test.xrtextview android:id="@+id/mytextview_tv" android:layout_width="match_parent" android:layout_height="wrap_content" /> <textview android:id="@+id/mytextview_tv1" android:layout_width="match_parent" android:layout_height="wrap_content" android:textcolor="@android:color/black" /> </linearlayout>
完整实例代码点击此处本站下载。
更多关于android相关内容感兴趣的读者可查看本站专题:《android开发入门与进阶教程》、《android service组件使用技巧总结》、《android基本组件用法总结》及《android控件用法总结》
希望本文所述对大家android程序设计有所帮助。