赞
踩
Android的TextView在显示文字的时候有个问题就是一行还没显示满就跳到下一行,原因是:
1) TextView在显示中文的时候 标点符号不能显示在一行的行首和行尾,如果一个标点符号刚好在一行的行尾,该标点符号就会连同前一个字符跳到下一行显示;
2)一个英文单词不能被显示在两行中( TextView在显示英文时,标点符号是可以放在行尾的,但英文单词也不能分开 );
如果只是想让标点符号可以显示在行尾,有一个简单的方法就是在标点符号后加一个空格,则该标点符号就可以显示在行尾了;
如果想要两端对齐的显示效果,有两种方法:
1)修改Android源代码;将frameworks/base/core/Java/android/text下的StaticLayout.java文件中的如下代码:
去掉就可以了。去掉后标点符号可以显示在行首和行尾,英文单词也可以被分开在两行中显示。
2)自定义View显示文本
网上就有达人采用自定义View来解决这个问题,我做了实验并总结了一下:
自定义View的步骤:
1)继承View类或其子类,例子继承了TextView类;
2)写构造函数,通过XML获取属性(这一步中可以自定义属性,见例程);
3)重写父类的某些函数,一般都是以on开头的函数,例子中重写了onDraw()和onMeasure()函数;
=========================StartCustomTextView.java=============================
=======================attrs.xml===============================
该文件是自定义的属性,放在工程的res/values下
=======================main.xml==========================
=======================Main.java=============================
转自:http://hi.baidu.com/java_rose/blog/item/2940a030d1ec7f3e96ddd847.html
另外一个人的自定义TextView,学习一下
4.1 可以封装一个自定义的textview,直接包含自动排版换行的功能:
1 package cc.snser.test; 2 3 import android.content.Context; 4 import android.graphics.Paint; 5 import android.text.TextUtils; 6 import android.util.AttributeSet; 7 import android.widget.TextView; 8 9 public class AutoSplitTextView extends TextView { 10 private boolean mEnabled = true; 11 12 public AutoSplitTextView(Context context) { 13 super(context); 14 } 15 16 public AutoSplitTextView(Context context, AttributeSet attrs) { 17 super(context, attrs); 18 } 19 20 public AutoSplitTextView(Context context, AttributeSet attrs, int defStyle) { 21 super(context, attrs, defStyle); 22 } 23 24 public void setAutoSplitEnabled(boolean enabled) { 25 mEnabled = enabled; 26 } 27 28 @Override 29 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 30 if (MeasureSpec.getMode(widthMeasureSpec) == MeasureSpec.EXACTLY 31 && MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.EXACTLY 32 && getWidth() > 0 33 && getHeight() > 0 34 && mEnabled) { 35 String newText = autoSplitText(this); 36 if (!TextUtils.isEmpty(newText)) { 37 setText(newText); 38 } 39 } 40 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 41 } 42 43 private String autoSplitText(final TextView tv) { 44 final String rawText = tv.getText().toString(); //原始文本 45 final Paint tvPaint = tv.getPaint(); //paint,包含字体等信息 46 final float tvWidth = tv.getWidth() - tv.getPaddingLeft() - tv.getPaddingRight(); //控件可用宽度 47 48 //将原始文本按行拆分 49 String [] rawTextLines = rawText.replaceAll("\r", "").split("\n"); 50 StringBuilder sbNewText = new StringBuilder(); 51 for (String rawTextLine : rawTextLines) { 52 if (tvPaint.measureText(rawTextLine) <= tvWidth) { 53 //如果整行宽度在控件可用宽度之内,就不处理了 54 sbNewText.append(rawTextLine); 55 } else { 56 //如果整行宽度超过控件可用宽度,则按字符测量,在超过可用宽度的前一个字符处手动换行 57 float lineWidth = 0; 58 for (int cnt = 0; cnt != rawTextLine.length(); ++cnt) { 59 char ch = rawTextLine.charAt(cnt); 60 lineWidth += tvPaint.measureText(String.valueOf(ch)); 61 if (lineWidth <= tvWidth) { 62 sbNewText.append(ch); 63 } else { 64 sbNewText.append("\n"); 65 lineWidth = 0; 66 --cnt; 67 } 68 } 69 } 70 sbNewText.append("\n"); 71 } 72 73 //把结尾多余的\n去掉 74 if (!rawText.endsWith("\n")) { 75 sbNewText.deleteCharAt(sbNewText.length() - 1); 76 } 77 78 return sbNewText.toString(); 79 } 80 }
1 package cc.snser.test; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 6 public class TestCActivity extends Activity { 7 private AutoSplitTextView mText; 8 9 @Override 10 protected void onCreate(Bundle savedInstanceState) { 11 super.onCreate(savedInstanceState); 12 13 setContentView(R.layout.testc); 14 15 mText = (AutoSplitTextView)findViewById(R.id.txt); 16 mText.setText("本文地址http://www.cnblogs.com/goagent/p/5159125.html本文地址啊本文。地址。啊http://www.cnblogs.com/goagent/p/5159125.html"); 17 } 18 }
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:background="@android:color/white" 6 android:orientation="vertical" > 7 8 <cc.snser.test.AutoSplitTextView 9 android:id="@+id/txt" 10 android:layout_width="match_parent" 11 android:layout_height="200dp" 12 android:layout_marginTop="11dp" 13 android:layout_marginLeft="11dp" 14 android:layout_marginRight="11dp" 15 android:background="@android:color/holo_blue_light" 16 android:textSize="20sp" 17 android:textColor="@android:color/black" /> 18 19 </LinearLayout>
4.2 实现悬挂缩进
1 private String autoSplitText(final TextView tv, final String indent) { 2 final String rawText = tv.getText().toString(); //原始文本 3 final Paint tvPaint = tv.getPaint(); //paint,包含字体等信息 4 final float tvWidth = tv.getWidth() - tv.getPaddingLeft() - tv.getPaddingRight(); //控件可用宽度 5 6 //将缩进处理成空格 7 String indentSpace = ""; 8 float indentWidth = 0; 9 if (!TextUtils.isEmpty(indent)) { 10 float rawIndentWidth = tvPaint.measureText(indent); 11 if (rawIndentWidth < tvWidth) { 12 while ((indentWidth = tvPaint.measureText(indentSpace)) < rawIndentWidth) { 13 indentSpace += " "; 14 } 15 } 16 } 17 18 //将原始文本按行拆分 19 String [] rawTextLines = rawText.replaceAll("\r", "").split("\n"); 20 StringBuilder sbNewText = new StringBuilder(); 21 for (String rawTextLine : rawTextLines) { 22 if (tvPaint.measureText(rawTextLine) <= tvWidth) { 23 //如果整行宽度在控件可用宽度之内,就不处理了 24 sbNewText.append(rawTextLine); 25 } else { 26 //如果整行宽度超过控件可用宽度,则按字符测量,在超过可用宽度的前一个字符处手动换行 27 float lineWidth = 0; 28 for (int cnt = 0; cnt != rawTextLine.length(); ++cnt) { 29 char ch = rawTextLine.charAt(cnt); 30 //从手动换行的第二行开始,加上悬挂缩进 31 if (lineWidth < 0.1f && cnt != 0) { 32 sbNewText.append(indentSpace); 33 lineWidth += indentWidth; 34 } 35 lineWidth += tvPaint.measureText(String.valueOf(ch)); 36 if (lineWidth <= tvWidth) { 37 sbNewText.append(ch); 38 } else { 39 sbNewText.append("\n"); 40 lineWidth = 0; 41 --cnt; 42 } 43 } 44 } 45 sbNewText.append("\n"); 46 } 47 48 //把结尾多余的\n去掉 49 if (!rawText.endsWith("\n")) { 50 sbNewText.deleteCharAt(sbNewText.length() - 1); 51 } 52 53 return sbNewText.toString(); 54 }
调用方式:
autoSplitText(tv, "1、");
悬挂缩进效果:
[转载请保留本文地址:http://www.cnblogs.com/snser/p/5159125.html]
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。