android中英文混排

 在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);
	}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值