Android重写textview排版
来源:互联网 发布:电脑抢票软件 编辑:程序博客网 时间:2024/05/17 06:29
又遇textview排版问题,记录备忘一下~
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 ="http://schemas.android.com/apk/res/android";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",18);//字体大小textColor = attrs.getAttributeIntValue(namespace,"textColor",Color.BLACK);//字体颜色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, inttextColor, 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;}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;}@Overrideprotected void onDraw(Canvas canvas) {// super.onDraw(canvas);View view=(View)this.getParent();textShowWidth=this.getMeasuredWidth()-paddingLeft -paddingRight;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 blocke1.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);}}
注:上面代码存在BUG,调用setHeight会导致刷新,从而重新调用ondraw,导致ondraw无限循环调用。应该在其他地方如(onlayout)中计算高度,然后setheight,并且在高度改变的时候重新计算(如settext)。
0 0
- Android重写textview排版
- Android TextView排版
- android textview 排版混乱问题
- Android自定义TextView排版优化
- android textview文字自动排版
- 诡异的Android TextView排版布局问题
- Android TextView 解决文字换行排版
- android textView 排版显示参差不齐的解决方法
- 解决Android的TextView排版问题
- android textview 自动换行 整齐排版
- android textview 自动换行 整齐排版
- Android 解决TextView排版参差不齐的问题
- android textview 自动换行 整齐排版
- Android TextView 自动排版工具类
- android---重写textview自适应屏幕分辨率,压缩字体大小
- android---重写textview实现跑马灯
- android---重写TextView实现跑马灯
- android 自定义textView,实现排版对齐和换行
- C# 字符串之 Split()、Join()使用
- 简单的外观模式
- onAnimationEnd not get called
- 常用资源整理(ing...)
- 程序中使用dimension中定义的尺寸大小不对的原因
- Android重写textview排版
- Eclipse中修改xml文件字体大小
- Android 4.2 Gson 报错 Class no find
- GCD
- Base64,DES,RSA,SHA1,MD5笔记
- VM.SWAPPINESS=0在最新内核中可能会OOM,MYSQL被意外KILL
- CCSpriteBatchNode使用限制
- 第三方jar包无法查看源码
- WebView loaddata中文乱码