您的位置:首页 > 移动开发 > Android开发

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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: