Android重写TextView实现文字整齐排版

移动开发 Android
TextView有时不能满足我们排版的需求,那么如何重新定义TextView以实现我们的想法呢?本文介绍了如何重写TextView实现文字整齐排版。

TextView有时不能满足我们排版的需求,那么如何重新定义TextView以实现我们的想法呢,以下是实现的代码:

  1.  //XRTextView类 
  2. package rong.android.test; 
  3. import org.json.JSONArray; 
  4. import org.json.JSONException; 
  5. import android.content.Context; 
  6. import android.graphics.Canvas; 
  7. import android.graphics.Color; 
  8. import android.graphics.Paint; 
  9. import android.util.AttributeSet; 
  10. import android.view.View; 
  11. import android.widget.TextView; 
  12. public class XRTextView extends TextView{ 
  13.  private final String namespace = "rong.android.TextView"
  14.  private String text; 
  15.  private float textSize; 
  16.  private float paddingLeft; 
  17.  private float paddingRight; 
  18.  private float marginLeft; 
  19.  private float marginRight; 
  20.  private int textColor; 
  21.  private JSONArray colorIndex; 
  22.  private Paint paint1 = new Paint(); 
  23.  private Paint paintColor = new Paint(); 
  24.  private float textShowWidth; 
  25.  private float Spacing = 0
  26.  private float LineSpacing = 1.3f;//行与行的间距 
  27.   
  28.  public XRTextView(Context context, AttributeSet attrs) { 
  29.   super(context, attrs); 
  30.   text = attrs.getAttributeValue( 
  31.     "http://schemas.android.com/apk/res/android""text"); 
  32.   textSize = attrs.getAttributeIntValue(namespace, "textSize"25);//字体大小 
  33.   textColor = attrs.getAttributeIntValue(namespace, "textColor",Color.BLUE);//字体颜色 
  34.   paddingLeft = attrs.getAttributeIntValue(namespace, "paddingLeft"0); 
  35.   paddingRight = attrs.getAttributeIntValue(namespace, "paddingRight"0); 
  36.   marginLeft = attrs.getAttributeIntValue(namespace, "marginLeft"0); 
  37.   marginRight = attrs.getAttributeIntValue(namespace, "marginRight"0); 
  38.   paint1.setTextSize(textSize); 
  39.   paint1.setColor(textColor); 
  40.   paint1.setAntiAlias(true); 
  41.   paintColor.setAntiAlias(true); 
  42.   paintColor.setTextSize(textSize); 
  43.   paintColor.setColor(Color.BLUE); 
  44.  } 
  45.  public XRTextView(Context context, float textSize, int textColor, float paddingLeft, float paddingRight, float marginLeft, float marginRight){ 
  46.   super(context); 
  47.   this.textSize = textSize; 
  48.   this.textColor = textColor; 
  49.   this.paddingLeft = paddingLeft; 
  50.   this.paddingRight = paddingRight; 
  51.   this.marginLeft = marginLeft; 
  52.   this.marginRight = marginRight; 
  53.   paint1.setTextSize(textSize); 
  54.   paint1.setColor(textColor); 
  55.   paint1.setAntiAlias(true); 
  56.   paintColor.setAntiAlias(true); 
  57.   paintColor.setTextSize(textSize); 
  58.   paintColor.setColor(Color.BLUE); 
  59.  } 
  60.   
  61.  public JSONArray getColorIndex() { 
  62.   return colorIndex; 
  63.  } 
  64.  public void setColorIndex(JSONArray colorIndex) { 
  65.   this.colorIndex = colorIndex; 
  66.  } 
  67.  /** 
  68.   * 传入一个索引,判断当前字是否被高亮 
  69.   * @param index 
  70.   * @return 
  71.   * @throws JSONException 
  72.   */ 
  73.  public boolean isColor(int index) throws JSONException{ 
  74.   if(colorIndex == null){ 
  75.    return false
  76.   } 
  77.   for(int i = 0 ; i < colorIndex.length() ; i ++){ 
  78.    JSONArray array = colorIndex.getJSONArray(i); 
  79.    int start = array.getInt(0); 
  80.    int end = array.getInt(1)-1
  81.    if(index >= start && index <= end){ 
  82.     return true
  83.    } 
  84.     
  85.   } 
  86.    
  87.    
  88.   return false
  89.  } 
  90.   
  91.  @Override 
  92.  protected void onDraw(Canvas canvas) { 
  93. //  super.onDraw(canvas); 
  94.   View view=(View)this.getParent(); 
  95.   textShowWidth=view.getMeasuredWidth()-paddingLeft - paddingRight - marginLeft - marginRight; 
  96.   int lineCount = 0
  97.    
  98.   text = this.getText().toString();//.replaceAll("\n", "\r\n"); 
  99.   if(text==null)return
  100.   char[] textCharArray = text.toCharArray(); 
  101.   // 已绘的宽度 
  102.   float drawedWidth = 0
  103.   float charWidth; 
  104.   for (int i = 0; i < textCharArray.length; i++) { 
  105.    charWidth = paint1.measureText(textCharArray, i, 1); 
  106.     
  107.    if(textCharArray[i]=='\n'){ 
  108.     lineCount++; 
  109.     drawedWidth = 0
  110.     continue
  111.    } 
  112.    if (textShowWidth - drawedWidth < charWidth) { 
  113.     lineCount++; 
  114.     drawedWidth = 0
  115.    } 
  116.    boolean color = false
  117.    try { 
  118.     color = isColor(i); 
  119.    } catch (JSONException e1) { 
  120.     // TODO Auto-generated catch block 
  121.     e1.printStackTrace(); 
  122.    } 
  123.     
  124.    if(color){ 
  125.      
  126.     canvas.drawText(textCharArray, i, 1, paddingLeft + drawedWidth, 
  127.       (lineCount + 1) * textSize * LineSpacing, paintColor); 
  128.    }else
  129.      
  130.     canvas.drawText(textCharArray, i, 1, paddingLeft + drawedWidth, 
  131.       (lineCount + 1) * textSize * LineSpacing, paint1); 
  132.    } 
  133.    if(textCharArray[i] > 127 && textCharArray[i] != '、' && textCharArray[i] != ',' && textCharArray[i] != '。' && textCharArray[i] != ':' && textCharArray[i] != '!'){ 
  134.     drawedWidth += charWidth + Spacing; 
  135.      
  136.    }else
  137.     drawedWidth += charWidth; 
  138.    } 
  139.   } 
  140.   setHeight((int) ((lineCount + 1) * (int) textSize * LineSpacing + 10)); 
  141.  } 
  142.  public float getSpacing() { 
  143.   return Spacing; 
  144.  } 
  145.  public void setSpacing(float spacing) { 
  146.   Spacing = spacing; 
  147.  } 
  148.  public float getMYLineSpacing() { 
  149.   return LineSpacing; 
  150.  } 
  151.  public void setMYLineSpacing(float lineSpacing) { 
  152.   LineSpacing = lineSpacing; 
  153.  } 
  154.  public float getMYTextSize() { 
  155.   return textSize; 
  156.  } 
  157.  public void setMYTextSize(float textSize) { 
  158.   this.textSize = textSize; 
  159.   paint1.setTextSize(textSize); 
  160.   paintColor.setTextSize(textSize); 
  161.  } 
  162.   
  163.   
  164.  
  165.   
  166.  
  167.   
  168. //MainActivity类 
  169. package rong.android.test; 
  170. import android.os.Bundle; 
  171. import android.widget.TextView; 
  172. import android.app.Activity; 
  173. public class MainActivity extends Activity { 
  174.  private XRTextView xrtextview = null
  175.  private TextView textview = null
  176.  private String content = "abcdefgABCDEF我要你lfwjkdfl;skjf asljkflskjfls;kjfsljfwfisdlfjsllkjsdfjlskjf546132s1f3sd4f31s3dffslfksjdfljlsadkjflsajdf sdfjklsajdflsa;jdfls 的!@#$%^&*()_"
  177.  @Override 
  178.  protected void onCreate(Bundle savedInstanceState) { 
  179.   super.onCreate(savedInstanceState); 
  180.   setContentView(R.layout.activity_main); 
  181.   xrtextview = (XRTextView) this.findViewById(R.id.mytextview_tv); 
  182.   xrtextview.setText(content); 
  183.   textview = (TextView) this.findViewById(R.id.mytextview_tv1); 
  184.   textview.setText(content); 
  185.  } 
  186. }  

 

  1. //布局文件 
  2. LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.    xmlns:tools="http://schemas.android.com/tools" 
  4.    android:layout_width="match_parent" 
  5.    android:layout_height="match_parent" 
  6.    android:orientation="vertical" > 
  7.    <rong.android.test.XRTextView 
  8.        android:id="@+id/mytextview_tv" 
  9.        android:layout_width="match_parent" 
  10.        android:layout_height="wrap_content" /> 
  11.    <TextView 
  12.        android:id="@+id/mytextview_tv1" 
  13.        android:layout_width="match_parent" 
  14.        android:layout_height="wrap_content" 
  15.        android:textColor="@android:color/black" /> 
  16. /LinearLayout>  

源码下载:百度盘

责任编辑:徐川 来源: OSChina
相关推荐

2013-03-28 15:47:53

TextView文字自

2010-09-06 13:21:04

TextViewAndroid

2017-04-20 12:45:08

AndroidTextView

2010-09-08 17:20:42

CSS

2013-04-07 10:09:00

Android开发TextView属性

2011-10-19 17:42:10

WPS 2012

2010-09-14 10:34:17

DIV CSS

2016-12-07 10:32:14

移动应用开发底部导航android

2010-09-01 14:20:19

CSS排版

2021-02-26 20:01:30

LaTex排版LaTeX排版

2011-09-14 11:31:26

Android API

2012-12-25 13:54:28

AndroidTextview

2010-09-10 10:09:26

Android

2010-09-10 14:54:12

DIV排版

2010-08-30 14:03:59

CSS

2009-12-30 15:26:02

Silverlight

2009-07-30 09:42:29

CSS实现文字旋转

2019-11-27 09:22:15

戴尔

2021-02-24 16:50:07

LaTex排版表格

2021-02-24 16:15:20

LaTeX排版LaTeX列表
点赞
收藏

51CTO技术栈公众号