原文地址http://blog.csdn.net/z609933542/article/details/53168738
看了這篇文章之后 確實 樓主寫的 要比上邊的一段好一些 后來發現還是有計算換行的問題。
發現這篇文章 http://www.lxweimin.com/p/d916a667c611
有說關于TextView 有標點符號換行問題
不過textview的折行包含以下規律:
1、半角字符與全角字符混亂所致:這種情況一般就是漢字與數字、英文字母混用。
2、TextView在顯示中文的時候標點符號不能顯示在一行的行首和行尾,如果一個標點符號剛好在一行的行尾,該標點符號就會連同前一個字符跳到下一行顯示。
3、一個英文單詞不能被顯示在兩行中( TextView在顯示英文時,標點符號是可以放在行尾的,但英文單詞也不能分開 )。
項目中我遇到了 半角全角問題
然后 需要轉一下 http://blog.csdn.net/ojiahao12/article/details/48104399
public static String ToDBC(String input) {
char c[] = input.toCharArray();
for (int i = 0; i < c.length; i++) {
if (c[i] == '\u3000') {<span style="white-space:pre"> </span>//空格
c[i] = ' ';
} else if (c[i] > '\uFF00' && c[i] < '\uFF5F') {<span style="white-space:pre"> </span>//半角與全角相差 65248
c[i] = (char) (c[i] - 65248);
}
}
return new String(c);
}
最后結合需求 這樣的一點小的改動
public class MTextView extends TextView {
private Context context;
public MTextView(Context context) {
super(context);
// TODO Auto-generated constructor stub
this.context = context;
}
public MTextView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
this.context = context;
}
public MTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
this.context = context;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int mode = MeasureSpec.getMode(heightMeasureSpec);
Layout layout = getLayout();
if (layout != null) {
int height = (int) Math.ceil(getMaxLineHeight(ToDBC(this.getText().toString()), mode))
+ getCompoundPaddingTop() + getCompoundPaddingBottom();
int width = getMeasuredWidth();
setMeasuredDimension(width, height);
}
}
private float getMaxLineHeight(String str, int mode) {
float height = 0.0f;
float width = getMeasuredWidth();
float widthPixels = context.getResources().getDisplayMetrics().widthPixels;
//這里具體this.getPaint()要注意使用,要看你的TextView在什么位置,
// 這個是拿TextView父控件的Padding的,為了更準確的算出換行
float pLeft = ((LinearLayout) getParent()).getPaddingLeft();
float pRight = ((LinearLayout) getParent()).getPaddingRight();
//檢測字符串中是否包含換行符,獲得換行的次數,在之后計算高度時加上
int br = 0;
if (str.contains("\n"))
br = str.split("\n").length - 1;
/**
* wrap_content/未指定寬度(MeasureSpec.UNSPECIFIED),則用屏幕寬度計算
* 否則就使用View自身寬度計算,并且無需計算Parent的Padding
*/
int line;
if (mode == MeasureSpec.UNSPECIFIED)
line = (int)
Math.ceil((this.getPaint().measureText(str) /
(widthPixels - getPaddingLeft() - pLeft - pRight - getPaddingRight())));
else {
line = (int)
Math.ceil((this.getPaint().measureText(str) /
(width - getPaddingLeft() - getPaddingRight())));
}
height = (this.getPaint().getFontMetrics().descent -
this.getPaint().getFontMetrics().ascent) * (line + br);
return height;
}
public static String ToDBC(String input) {
char c[] = input.toCharArray();
for (int i = 0; i < c.length; i++) {
if (c[i] == '\u3000') {<span style="white-space:pre"> </span>//空格
c[i] = ' ';
} else if (c[i] > '\uFF00' && c[i] < '\uFF5F') {<span style="white-space:pre"> </span>//半角與全角相差 65248
c[i] = (char) (c[i] - 65248);
}
}
return new String(c);
}
}