处理TextView 文字排版乱的文体,

相信很多做android的都会遇到文体,就是TextView中文字很多的时候,尤其是包括数字,字母等时,排版会很乱,莫名的出现空格等内容。这里是我从网上找的解决方法,还有就是使用过程中的心的,整理给大家,本人菜鸟一枚 不断学习中 也希望大家多多指教。由于找不到原文地址,这里就不加链接了,但是非常感谢提供此文章和方法的前辈。


网上对处理排版混乱的问题有两种解决方法,第一就是重写TextView 第二就是将全角,半角的文字转化为同一种。这里采用的第一种方法。首先就是重写TextView的类。

XRTextView.ava

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 XRTextView extends TextView{
	private final String namespace = "rong.android.TextView";
	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 XRTextView(Context context, AttributeSet attrs) {
		super(context, attrs);
		text = attrs.getAttributeValue(
				"http://schemas.android.com/apk/res/android", "text");
		textSize = attrs.getAttributeIntValue(namespace, "textSize", 25);//字体大小
		textColor = attrs.getAttributeIntValue(namespace, "textColor",Color.BLUE);//字体颜色
		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 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) {
//		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);
	}
	
	
}
接下来就是在XML文件中使用重写的TextView。

<com.zsq.XRTextView
        android:id="@+id/mytextview_tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />


最后就是在Activity类中声明和使用。

public class MainActivity extends Activity {
	private XRTextView xrtextview = null;
	private TextView textview = null;
	private String content = "说明:\n1.初始化前请对兔舍栋数,按A、B、C······Z的顺序对兔舍栋数编号。2.对每栋兔舍内排数按数字编号1-10。3.对每栋兔舍内每排笼位按数字编号001、002---此排所有兔笼。4.此初始化,为兔笼初始设置,为了方便以后兔场扩建,建议提前对兔笼增加预设。5.如果兔场内兔子的编号数字1-500或者1--......,可以选择1栋1排,笼数选择500或者.....,兔笼内的兔子就是按照1-......编号。7.如有不明之处,可联系本公司客服电话18560501752";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		xrtextview = (XRTextView) this.findViewById(R.id.mytextview_tv);
		xrtextview.setText(content);
		
		textview = (TextView) this.findViewById(R.id.mytextview_tv1);
		textview.setText(content);
	}

}

接下来就是说说在使用中遇到的问题,首先我们在textview中会 为了对齐等效果会经常使用android:marginLeft.或者android:marginRight等,我当时也是,直接写到布局文件里,但是后来发现会出现一半的文字,最后看了下上面重写的<span style="font-family: Arial, Helvetica, sans-serif;">XRTextView类。里面已经设置果</span><span style="font-family: Arial, Helvetica, sans-serif;">marginLeft等属性,而且还是参与到换行计算中,所以这里要想达到一定的对齐或者美观效果,智能通过这个类设置了。 总之,用这种方法还是很好用的,推荐大家使用。</span>


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值