Android重写TextView实现文字整齐排版的方法(附demo源码下载)

 更新时间:2016年02月18日 10:57:22   作者:xurong  
这篇文章主要介绍了Android重写TextView实现文字整齐排版的方法,结合实例形式分析了Android重写TextView实现文字整齐排版的相关技巧,并附带demo源码供读者下载参考,需要的朋友可以参考下

本文实例讲述了Android重写TextView实现文字整齐排版的方法。分享给大家供大家参考,具体如下:

XRTextView类

package rong.android.test;
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);
 }
}

MainActivity类

package rong.android.test;
import android.os.Bundle;
import android.widget.TextView;
import android.app.Activity;
public class MainActivity extends Activity {
 private XRTextView xrtextview = null;
 private TextView textview = null;
 private String content = "abcdefgABCDEF我要你lfwjkdfl;skjf asljkflskjfls;kjfsljfwfisdlfjsllkjsdfjlskjf546132s1f3sd4f31s3dffslfksjdfljlsadkjflsajdf sdfjklsajdflsa;jdfls 的!@#$%^&*()_";
 @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);
 }
}

布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" >
  <rong.android.test.XRTextView
    android:id="@+id/mytextview_tv"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
  <TextView
    android:id="@+id/mytextview_tv1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textColor="@android:color/black" />
</LinearLayout>

完整实例代码点击此处本站下载

更多关于Android相关内容感兴趣的读者可查看本站专题:《Android开发入门与进阶教程》、《Android Service组件使用技巧总结》、《Android基本组件用法总结》及《Android控件用法总结

希望本文所述对大家Android程序设计有所帮助。

相关文章

  • Android中Glide加载圆形图片和圆角图片实例代码

    Android中Glide加载圆形图片和圆角图片实例代码

    本篇文章主要介绍了Android中Glide加载圆形图片和圆角图片实例代码,具体一定的参考价值,有兴趣的可以了解一下
    2017-05-05
  • Android RecyclerView实现吸顶动态效果流程分析

    Android RecyclerView实现吸顶动态效果流程分析

    RecyclerView是Android一个更强大的控件,其不仅可以实现和ListView同样的效果,还有优化了ListView中的各种不足。其可以实现数据纵向滚动,也可以实现横向滚动(ListView做不到横向滚动)。接下来讲解RecyclerView的用法
    2022-12-12
  • Android自定义View控件实现多种水波纹涟漪扩散效果

    Android自定义View控件实现多种水波纹涟漪扩散效果

    这篇文章主要给大家介绍了关于Android自定义View控件实现多种水波纹涟漪扩散效果的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
    2018-03-03
  • Android Notification使用教程详解

    Android Notification使用教程详解

    这篇文章主要介绍了Android Notification使用,通知的使用的内容还是比较多的,此篇文章将会尽可能详细的介绍Notification的内容,需要的朋友可以参考下
    2022-07-07
  • Android利用RecyclerView实现列表倒计时效果

    Android利用RecyclerView实现列表倒计时效果

    这篇文章主要为大家详细介绍了Android利用RecyclerView实现列表倒计时效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-09-09
  • Android本地存储方法浅析介绍

    Android本地存储方法浅析介绍

    这篇文章主要介绍了Android本地存储案例,方法简单可以实现存储并达到节省内存的效果,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习吧
    2022-10-10
  • 一文详解如何在Flutter中使用导航Navigator

    一文详解如何在Flutter中使用导航Navigator

    一个APP如果没有页面跳转那么是没有灵魂的,页面跳转的一个常用说法就是Navigator。那么在flutter中如何使用Navigator呢?本文就来和大家详细聊聊
    2023-02-02
  • Android仿QQ可拉伸头部控件

    Android仿QQ可拉伸头部控件

    这篇文章主要为大家详细介绍了Android仿QQ可拉伸头部控件,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-11-11
  • Android编程之ICS式下拉菜单PopupWindow实现方法详解(附源码下载)

    Android编程之ICS式下拉菜单PopupWindow实现方法详解(附源码下载)

    这篇文章主要介绍了Android编程之ICS式下拉菜单PopupWindow实现方法,结合实例详细分析了ICS式下拉菜单的实现原理与相关技巧,并附带源码供读者下载,需要的朋友可以参考下
    2015-12-12
  • android 实现APP中改变头像图片的实例代码

    android 实现APP中改变头像图片的实例代码

    这篇文章主要介绍了android 实现APP中改变头像图片的实例代码,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-07-07

最新评论