Created
July 20, 2012 07:16
-
-
Save dzwillpower/3149218 to your computer and use it in GitHub Desktop.
解决Android下面TextView显示长度的问题 通过计算每个字符的宽度
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
* @param v 需要处理的textview | |
* @param content 要处理的类容 | |
* @param show_len 需要显示的长度 | |
* @return | |
*/ | |
public static String dealDetailString(TextView v,String content,float show_len){ | |
TextPaint tpaint =v.getPaint(); | |
String temp=""; | |
if(content!=null){ | |
temp=content.replaceAll("\n", " ").replaceAll("\b", " "); | |
} | |
String str_content=(content==null?"":temp); | |
float len=0; | |
int s_len=0; | |
float totallength=0; | |
if(str_content!=null&&str_content!=""){ | |
len = tpaint.measureText(str_content); | |
totallength=tpaint.measureText(str_content); | |
System.out.println("==================== len: "+len); | |
s_len=str_content.length(); | |
} | |
for(int i=0;i<s_len&&len>show_len;i++){ | |
str_content=str_content.substring(0, str_content.length()-1); | |
len = tpaint.measureText(str_content); | |
} | |
if(totallength>show_len) | |
{ | |
return str_content+"..."; | |
} | |
else | |
{ | |
return str_content; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment