解决TextView排版不齐问题----自定义TextVIew

解决TextView排版不齐问题----自定义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 MarginTextView 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 MarginTextView(Context context, AttributeSet attrs) {
		super(context, attrs);
		text = attrs.getAttributeValue(
				namespace, "text");
		/**资源id---形式为@2130968673*/
		String  color_resource_id=attrs.getAttributeValue(namespace, "textColor");
		/**去掉资源id前面的@*/
		String resource_id2=color_resource_id.substring(1);
		System.out.println("color_name==="+resource_id2+",resource_id="+color_resource_id);
		
		/***
		 * 根据R文件中的颜色的资源id获取颜色对应的int数值
		 */
		int color_int=getResources().getColor(Integer.parseInt(resource_id2));
		System.out.println("color_int=="+color_int);
		
		String textSize_yuanshi=attrs.getAttributeValue(namespace, "textSize");
		
		String textSize_2=textSize_yuanshi.substring(0, textSize_yuanshi.indexOf("."));
		System.out.println("textSize_yuanshi="+textSize_yuanshi+",textSize_2=="+textSize_2);
		textSize=Integer.parseInt(textSize_2);//字体大小
		
		 
		
		textColor=color_int;
		System.out.println("textColor================"+textColor);//-8388480
		
		
		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.WHITE);
	}
	public MarginTextView(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.WHITE);
	}

	
	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=view.getMeasuredWidth()-paddingLeft - paddingRight - marginLeft - marginRight;
		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 block
				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);
			}
			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);
	}
	
	
}

将网上的代码稍微整理了一下,可以使用。但是设置字体的大小上与原来的TextView的设置有些区别,表现在:如果都设置成12.0sp,这个地方的MarginTextView会显得字很小。
原因还没有去查找。

你可能感兴趣的:(Android)