Android重写TextView实现文字整齐排版的方法(附demo源码下载)
本文实例讲述了Android重写TextView实现文字整齐排版的方法。分享给大家供大家参考,具体如下:
XRTextView类
packagerong.android.test;
importorg.json.JSONArray;
importorg.json.JSONException;
importandroid.content.Context;
importandroid.graphics.Canvas;
importandroid.graphics.Color;
importandroid.graphics.Paint;
importandroid.util.AttributeSet;
importandroid.view.View;
importandroid.widget.TextView;
publicclassXRTextViewextendsTextView{
privatefinalStringnamespace="rong.android.TextView";
privateStringtext;
privatefloattextSize;
privatefloatpaddingLeft;
privatefloatpaddingRight;
privatefloatmarginLeft;
privatefloatmarginRight;
privateinttextColor;
privateJSONArraycolorIndex;
privatePaintpaint1=newPaint();
privatePaintpaintColor=newPaint();
privatefloattextShowWidth;
privatefloatSpacing=0;
privatefloatLineSpacing=1.3f;//行与行的间距
publicXRTextView(Contextcontext,AttributeSetattrs){
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);
}
publicXRTextView(Contextcontext,floattextSize,inttextColor,floatpaddingLeft,floatpaddingRight,floatmarginLeft,floatmarginRight){
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);
}
publicJSONArraygetColorIndex(){
returncolorIndex;
}
publicvoidsetColorIndex(JSONArraycolorIndex){
this.colorIndex=colorIndex;
}
/**
*传入一个索引,判断当前字是否被高亮
*@paramindex
*@return
*@throwsJSONException
*/
publicbooleanisColor(intindex)throwsJSONException{
if(colorIndex==null){
returnfalse;
}
for(inti=0;i<colorIndex.length();i++){
JSONArrayarray=colorIndex.getJSONArray(i);
intstart=array.getInt(0);
intend=array.getInt(1)-1;
if(index>=start&&index<=end){
returntrue;
}
}
returnfalse;
}
@Override
protectedvoidonDraw(Canvascanvas){
//super.onDraw(canvas);
Viewview=(View)this.getParent();
textShowWidth=view.getMeasuredWidth()-paddingLeft-paddingRight-marginLeft-marginRight;
intlineCount=0;
text=this.getText().toString();//.replaceAll("\n","\r\n");
if(text==null)return;
char[]textCharArray=text.toCharArray();
//已绘的宽度
floatdrawedWidth=0;
floatcharWidth;
for(inti=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;
}
booleancolor=false;
try{
color=isColor(i);
}catch(JSONExceptione1){
//TODOAuto-generatedcatchblock
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));
}
publicfloatgetSpacing(){
returnSpacing;
}
publicvoidsetSpacing(floatspacing){
Spacing=spacing;
}
publicfloatgetMYLineSpacing(){
returnLineSpacing;
}
publicvoidsetMYLineSpacing(floatlineSpacing){
LineSpacing=lineSpacing;
}
publicfloatgetMYTextSize(){
returntextSize;
}
publicvoidsetMYTextSize(floattextSize){
this.textSize=textSize;
paint1.setTextSize(textSize);
paintColor.setTextSize(textSize);
}
}
MainActivity类
packagerong.android.test;
importandroid.os.Bundle;
importandroid.widget.TextView;
importandroid.app.Activity;
publicclassMainActivityextendsActivity{
privateXRTextViewxrtextview=null;
privateTextViewtextview=null;
privateStringcontent="abcdefgABCDEF我要你lfwjkdfl;skjfasljkflskjfls;kjfsljfwfisdlfjsllkjsdfjlskjf546132s1f3sd4f31s3dffslfksjdfljlsadkjflsajdfsdfjklsajdflsa;jdfls的!@#$%^&*()_";
@Override
protectedvoidonCreate(BundlesavedInstanceState){
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);
}
}
布局文件
<LinearLayoutxmlns: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开发入门与进阶教程》、《AndroidService组件使用技巧总结》、《Android基本组件用法总结》及《Android控件用法总结》
希望本文所述对大家Android程序设计有所帮助。