android中英文混排
2015-12-06 13:40
399 查看
在textview上面有中文的英文的时候会出现莫名的换行,初始想着通过变成全角解决,发现全角不能全部解决。在网上查看的解决办法,可以重写textview解决,但是会有一点小的问题,修复了这个问题。
public class XRTextView extends TextView { private final String namespace = "rong.android.TextView"; private String text; private float textSize; 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.1f;// private Context context; public XRTextView(Context context, AttributeSet attrs) { super(context, attrs); this.context = context; textSize = super.getTextSize(); paint1.setTextSize(textSize); textColor = super.getTextColors().getColorForState(getDrawableState(), 0); paint1.setColor(textColor); paint1.setAntiAlias(true); } 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 = getWidth(); 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) { e1.printStackTrace(); } if (color) { canvas.drawText(textCharArray, i, 1, drawedWidth, (lineCount + 1) * textSize * LineSpacing, paintColor); } else { canvas.drawText(textCharArray, i, 1, 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); } }
相关文章推荐
- android典型代码系列(二十八)------通话记录的操作
- android典型代码系列(二十七)------锁屏打开浏览器传数据,解屏最小化浏览器
- android典型代码系列(二十六)------App widget的使用
- android典型代码系列(二十五)------popupwindow的使用
- android典型代码系列(二十四)------获取某个应用程序所占用的内存
- android典型代码系列(二十二)------按键使上面的EditText抖动
- Android设置Settings:PreferenceFragment(2)
- android数据存储方式(一)
- Android Studio下载地址
- Android官方文档翻译 十一 2.4Overlaying the Action Bar
- android学习之路(七)---- 用Fan-Image-Loader实现一个炫酷的相册功能
- Android Handler
- AOSP git/repo
- Android通过adb命令来获取手机当前页面的名称
- android两种方式实现离线缓存
- Android Studio 无法关联到 svn
- Android应用中图片资源尺寸
- Android源码下载
- 超详细图文讲解android studio导入第三方类库的方法
- android-ndk-r10 编译 curl