TextView 因为特殊字符(半角圆角等问题)在折行的时候各种不爽 ,不规则折行
2016-08-16 15:31
363 查看
项目当中显示一个金额的范围 类似这种 ¥1200.00-¥1300.00 然后这行数据在显示的时候不能再一行全部显示需要换行,但是换行是从第二个¥开始的 然后第一行后面其实还能写,这就悲剧了,没看法百度了一下需要动源码,尼玛这我就不能忍了,后来还是自定义写了一下TextView 有点点 xiaoBUG 但是问题不大,展示少量文字还是可以的
贴代码 不费话
贴代码 不费话
import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.os.Build; import android.text.TextUtils; import android.util.AttributeSet; import android.widget.TextView; import org.json.JSONArray; import org.json.JSONException; import java.lang.reflect.Field; 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 int mLineCount=-1; private float LineSpacing = 1.3f;//行与行的间距 public XRTextView(Context context, AttributeSet attrs) { super(context, attrs); text = attrs.getAttributeValue( namespace, "text"); textSize = attrs.getAttributeIntValue(namespace, "textSize", dip2px(context, 10));//字体大小 textColor = attrs.getAttributeIntValue(namespace, "textColor", Color.rgb(255, 98, 17));//字体颜色255 69 0 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 static int dip2px(Context context, float dpValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (dpValue * scale + 0.5f); } 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) { // textShowWidth = getMeasuredWidth() - paddingLeft - paddingRight - marginLeft - marginRight; int lineCount = 0; text = this.getText()+""; if (TextUtils.isEmpty(text)) return; char[] textCharArray = text.toCharArray(); // 已绘的宽度 float drawedWidth = 0; float charWidth; int length=textCharArray.length; for (int i = 0; i < 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) { 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); }*/ 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; } } } 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); } public void onMeasure(int w,int h){ super.onMeasure(w, h); setMeasuredDimension(getMeasuredWidth(), (int) ((measureLineCount() + 1) * (int) textSize * LineSpacing + 10)); } private int measureLineCount(){ if(mLineCount!=-1){ return mLineCount; } if(Build.VERSION.SDK_INT>=16){ try { LineSpacing=getLineSpacingMultiplier(); } catch (Exception e) { e.printStackTrace(); } }else{ try { Field field = this.getClass().getDeclaredField("LineSpacing"); field.setAccessible(true); LineSpacing=field.getFloat(this); } catch (Exception e) { e.printStackTrace(); } } textShowWidth = getMeasuredWidth() - paddingLeft - paddingRight - marginLeft - marginRight; int lineCount = 0; text = this.getText()+""; if (TextUtils.isEmpty(text)) return 0; char[] textCharArray = text.toCharArray(); // 已绘的宽度 float drawedWidth = 0; float charWidth; int length=textCharArray.length; for (int i = 0; i < length; i++) { charWidth = paint1.measureText(textCharArray, i, 1); if (textCharArray[i] == '\n') { lineCount++; drawedWidth = 0; continue; } if (textShowWidth - drawedWidth < charWidth) { lineCount++; drawedWidth = 0; } if (textCharArray[i] > 127 && textCharArray[i] != '、' && textCharArray[i] != ',' && textCharArray[i] != '。' && textCharArray[i] != ':' && textCharArray[i] != '!') { drawedWidth += charWidth + Spacing; } else { drawedWidth += charWidth; } } mLineCount=lineCount; return lineCount; } }
相关文章推荐
- 请求Http时可能因为字符问题导致错误的特殊字符转码
- jQuery在异步请求数据返回后,调用$("selector").html(data.content);之后因为一些特殊字符或者',"不能显示内容的问题解决办法
- 解决gson转json字符串的时候=号等特殊字符会出现乱,导致加密结果不正确的问题
- 注意Vietnamese_CI_AS排序规则下的特殊字符大小敏感问题
- XML解析时候因为特殊字符&,<,",'出错
- XML解析时候因为特殊字符&,<,>,",'出错
- 因为公司的产品用asp开发, 前一段时间用asp写了一个生成静态页面并分页的程序,但缘于对.net的热爱,写了这个.net下的生成静态页面并分页的程序。 主要的原理就是替换模板里的特殊字符。
- 解决Rss/Xml中一不能被识别的特殊字符问题
- oracle插入特殊字符'&'问题
- oracle插入特殊字符'&'问题
- url中的特殊字符问题
- SQL Server中全角和半角字符的比较问题
- VC读取mysql数据库中文字符的时候全部显示问号问题的处理
- XML特殊字符问题
- 特殊字符变乱码问题如何解决呢?
- Xml数据中加入空格,或特殊字符的问题
- 特殊字符不能显示的问题
- SQL Server中全角和半角字符的比较问题
- 特殊字符转换问题
- 汉字与字符混合单元格折行的问题