原文地址http://blog.csdn.net/z609933542/article/details/53168738
看了這篇文章之后 確實(shí) 樓主寫的 要比上邊的一段好一些 后來發(fā)現(xiàn)還是有計(jì)算換行的問題距淫。
發(fā)現(xiàn)這篇文章 http://www.reibang.com/p/d916a667c611
有說關(guān)于TextView 有標(biāo)點(diǎn)符號換行問題
不過textview的折行包含以下規(guī)律:
1绞绒、半角字符與全角字符混亂所致:這種情況一般就是漢字與數(shù)字、英文字母混用榕暇。
2蓬衡、TextView在顯示中文的時(shí)候標(biāo)點(diǎn)符號不能顯示在一行的行首和行尾,如果一個(gè)標(biāo)點(diǎn)符號剛好在一行的行尾彤枢,該標(biāo)點(diǎn)符號就會連同前一個(gè)字符跳到下一行顯示狰晚。
3、一個(gè)英文單詞不能被顯示在兩行中( TextView在顯示英文時(shí)堂污,標(biāo)點(diǎn)符號是可以放在行尾的家肯,但英文單詞也不能分開 )龄砰。
項(xiàng)目中我遇到了 半角全角問題
然后 需要轉(zhuǎn)一下 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);
}
最后結(jié)合需求 這樣的一點(diǎn)小的改動
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在什么位置,
// 這個(gè)是拿TextView父控件的Padding的换棚,為了更準(zhǔn)確的算出換行
float pLeft = ((LinearLayout) getParent()).getPaddingLeft();
float pRight = ((LinearLayout) getParent()).getPaddingRight();
//檢測字符串中是否包含換行符,獲得換行的次數(shù)式镐,在之后計(jì)算高度時(shí)加上
int br = 0;
if (str.contains("\n"))
br = str.split("\n").length - 1;
/**
* wrap_content/未指定寬度(MeasureSpec.UNSPECIFIED),則用屏幕寬度計(jì)算
* 否則就使用View自身寬度計(jì)算,并且無需計(jì)算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);
}
}