android 自定义textView,实现排版对齐和换行

转自:http://blog.csdn.net/dyc333236081818/article/details/7467835

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

下面贴上我的代码:

main.xml

  1. <span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:ae="http://www.angellecho.com/"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:background="@android:color/black"  
  7.     android:gravity="center_horizontal"  
  8.     android:orientation="vertical" >  
  9.   
  10.     <ScrollView   
  11.         android:layout_width="wrap_content"  
  12.         android:layout_height="200dp" >  
  13.     <com.wigit.MyTextView2  
  14.         android:id="@+id/view"  
  15.         android:layout_width="fill_parent"  
  16.         android:layout_height="200dp"  
  17.         android:layout_marginTop="20dp"  
  18.         android:layout_marginLeft="20dp"  
  19.         android:layout_marginRight="20dp"  
  20.         android:background="@android:color/darker_gray"  
  21.         ae:marginLeft="20"  
  22.         ae:marginRight="20"  
  23.         ae:paddingLeft="20"  
  24.         ae:paddingRight="20"  
  25.         ae:textColor="#FFFFFF"  
  26.         ae:textSize="30" />  
  27.     </ScrollView>  
  28. </LinearLayout></span>  
重写后的textview如下,可能与网上其他例子有些相似,但是还解决了drawText不能换行的问题:

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

测试activity
  1. package com.text;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.IOException;  
  5. import java.io.InputStream;  
  6. import java.io.InputStreamReader;  
  7.   
  8. import com.wigit.MyTextView2;  
  9.   
  10. import android.app.Activity;  
  11. import android.content.Context;  
  12. import android.content.res.AssetManager;  
  13. import android.os.Bundle;  
  14. import android.text.method.ScrollingMovementMethod;  
  15.   
  16. public class MTextViewActivity extends Activity {  
  17.     MyTextView2 view;  
  18.     /** Called when the activity is first created. */  
  19.     @Override  
  20.     public void onCreate(Bundle savedInstanceState) {  
  21.         super.onCreate(savedInstanceState);  
  22.         setContentView(R.layout.main1);  
  23.           
  24.         view = (MyTextView2) findViewById(R.id.view);  
  25.         view.setText(getAssetsString(this,"1.txt"));  
  26.         view.setMovementMethod(ScrollingMovementMethod.getInstance());  
  27.     }  
  1. <span style="white-space:pre">  </span>//读取assets文件夹下文本字符串  
  2.     public String getAssetsString(Context context,String fileName){  
  3.         StringBuffer sb = new StringBuffer();  
  4.         try {  
  5.             AssetManager am = context.getAssets();  
  6.             InputStream in = am.open(fileName);  
  7.             BufferedReader reader = new BufferedReader(new InputStreamReader(in));  
  8.             String line;  
  9.             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. }  

并附上例子如下(免积分哦):
http://download.csdn.net/detail/dyc333236081818/4231656
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值