---
title: " android 自定义textView，实现排版对齐和换行"
author: "Java-Object"
date: "2014-06-18T16:04:45Z"
tags: []
categories: ["编程语言"]
slug: "android-textview"
draft: false
lastmod: "2014-06-18T16:04:45Z"
id: 2082212
url: "https://hezd.iteye.com/blog/2082212"
---

转载自：[http://blog.csdn.net/dyc333236081818/article/details/7467835](http://blog.csdn.net/dyc333236081818/article/details/7467835)

android开发中的textview可以自动换行,但是对于显示纯英文文字来说很好用，如果夹杂了中文字符后，全角字符和半角字符混在一块儿，就会出现文字排版参差不齐，超级难看，这就需要重写textview来实现我们需要的显示方式。

下面贴上我的代码：

main.xml

 

**\[html]** [view plain](http://blog.csdn.net/dyc333236081818/article/details/7467835 "view plain")[copy](http://blog.csdn.net/dyc333236081818/article/details/7467835 "copy")[print](http://blog.csdn.net/dyc333236081818/article/details/7467835 "print")[?](http://blog.csdn.net/dyc333236081818/article/details/7467835 "?")

 

01. &lt;span style="font-size:18px;"&gt;&lt;?xml version="1.0" encoding="utf-8"?&gt;
02. &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
03. xmlns:ae="http://www.angellecho.com/"
04. android:layout\_width="fill\_parent"
05. android:layout\_height="fill\_parent"
06. android:background="@android:color/black"
07. android:gravity="center\_horizontal"
08. android:orientation="vertical" &gt;
09. &lt;ScrollView
10. android:layout\_width="wrap\_content"
11. android:layout\_height="200dp" &gt;
12. &lt;com.wigit.MyTextView2
13. android:id="@+id/view"
14. android:layout\_width="fill\_parent"
15. android:layout\_height="200dp"
16. android:layout\_marginTop="20dp"
17. android:layout\_marginLeft="20dp"
18. android:layout\_marginRight="20dp"
19. android:background="@android:color/darker\_gray"
20. ae:marginLeft="20"
21. ae:marginRight="20"
22. ae:paddingLeft="20"
23. ae:paddingRight="20"
24. ae:textColor="#FFFFFF"
25. ae:textSize="30" /&gt;
26. &lt;/ScrollView&gt;
27. &lt;/LinearLayout&gt;&lt;/span&gt;

重写后的textview如下，可能与网上其他例子有些相似，但是还解决了drawText不能换行的问题：

 

 

**\[java]** [view plain](http://blog.csdn.net/dyc333236081818/article/details/7467835 "view plain")[copy](http://blog.csdn.net/dyc333236081818/article/details/7467835 "copy")[print](http://blog.csdn.net/dyc333236081818/article/details/7467835 "print")[?](http://blog.csdn.net/dyc333236081818/article/details/7467835 "?")

 

01. package com.wigit;
02. import android.app.Activity;
03. import android.content.Context;
04. import android.graphics.Canvas;
05. import android.graphics.Color;
06. import android.graphics.Paint;
07. import android.util.AttributeSet;
08. import android.widget.TextView;
09. public class MyTextView2 extends TextView{
10. private final String namespace = "http://www.angellecho.com/";
11. private String text;
12. private float textSize;
13. private float paddingLeft;
14. private float paddingRight;
15. private float marginLeft;
16. private float marginRight;
17. private int textColor;
18. private Paint paint1 = new Paint();
19. private float textShowWidth;
20. public MyTextView2(Context context, AttributeSet attrs) {
21. super(context, attrs);
22. text = attrs.getAttributeValue(
23. "http://schemas.android.com/apk/res/android", "text");
24. textSize = attrs.getAttributeIntValue(namespace, "textSize", 15);
25. textColor = attrs.getAttributeIntValue(namespace, "textColor",Color.WHITE);
26. paddingLeft = attrs.getAttributeIntValue(namespace, "paddingLeft", 0);
27. paddingRight = attrs.getAttributeIntValue(namespace, "paddingRight", 0);
28. marginLeft = attrs.getAttributeIntValue(namespace, "marginLeft", 0);
29. marginRight = attrs.getAttributeIntValue(namespace, "marginRight", 0);
30. paint1.setTextSize(textSize);
31. paint1.setColor(textColor);
32. paint1.setAntiAlias(true);
33. textShowWidth = ((Activity) context).getWindowManager().getDefaultDisplay().getWidth() - paddingLeft - paddingRight - marginLeft - marginRight;
34. }
35. @Override
36. protected void onDraw(Canvas canvas) {
37. //super.onDraw(canvas);
38. int lineCount = 0;
39. text = this.getText().toString();//.replaceAll("\\n", "\\r\\n");
40. if(text==null)return;
41. char\[] textCharArray = text.toCharArray();
42. // 已绘的宽度
43. float drawedWidth = 0;
44. float charWidth;
45. for (int i = 0; i &lt; textCharArray.length; i++) {
46. charWidth = paint1.measureText(textCharArray, i, 1);
47. if(textCharArray\[i]=='\\n'){
48. lineCount++;
49. drawedWidth = 0;
50. continue;
51. }
52. if (textShowWidth - drawedWidth &lt; charWidth) {
53. lineCount++;
54. drawedWidth = 0;
55. }
56. canvas.drawText(textCharArray, i, 1, paddingLeft + drawedWidth,
57. (lineCount + 1) * textSize, paint1);
58. drawedWidth += charWidth;
59. }
60. setHeight((lineCount + 1) * (int) textSize + 5);
61. }
62. }

测试activity

**\[java]** [view plain](http://blog.csdn.net/dyc333236081818/article/details/7467835 "view plain")[copy](http://blog.csdn.net/dyc333236081818/article/details/7467835 "copy")[print](http://blog.csdn.net/dyc333236081818/article/details/7467835 "print")[?](http://blog.csdn.net/dyc333236081818/article/details/7467835 "?")

 

01. package com.text;
02. import java.io.BufferedReader;
03. import java.io.IOException;
04. import java.io.InputStream;
05. import java.io.InputStreamReader;
06. import com.wigit.MyTextView2;
07. import android.app.Activity;
08. import android.content.Context;
09. import android.content.res.AssetManager;
10. import android.os.Bundle;
11. import android.text.method.ScrollingMovementMethod;
12. public class MTextViewActivity extends Activity {
13. MyTextView2 view;
14. /\** Called when the activity is first created. \*/
15. @Override
16. public void onCreate(Bundle savedInstanceState) {
17. super.onCreate(savedInstanceState);
18. setContentView(R.layout.main1);
19. view = (MyTextView2) findViewById(R.id.view);
20. view.setText(getAssetsString(this,"1.txt"));
21. view.setMovementMethod(ScrollingMovementMethod.getInstance());
22. }

**\[java]** [view plain](http://blog.csdn.net/dyc333236081818/article/details/7467835 "view plain")[copy](http://blog.csdn.net/dyc333236081818/article/details/7467835 "copy")[print](http://blog.csdn.net/dyc333236081818/article/details/7467835 "print")[?](http://blog.csdn.net/dyc333236081818/article/details/7467835 "?")

 

01. &lt;span style="white-space:pre"&gt;  &lt;/span&gt;//读取assets文件夹下文本字符串
02. public String getAssetsString(Context context,String fileName){
03. StringBuffer sb = new StringBuffer();
04. try {
05. AssetManager am = context.getAssets();
06. InputStream in = am.open(fileName);
07. BufferedReader reader = new BufferedReader(new InputStreamReader(in));
08. String line;
09. while((line = reader.readLine())!=null){
10. line += ("\\n");
11. sb.append(line);
12. }
13. reader.close();
14. in.close();
15. } catch (IOException e) {
16. e.printStackTrace();
17. }
18. return sb.toString();
19. }
20. }