我们都知道通过指定android:ellipsize="end" android:singleLine="true" 可以让TextView自动截断超出部分并且添加省略号。但是如何判断一个TextView是否被截断了呢?
这个问题在StackOverflow上有人讨论过,不过遗憾的是我测试出Layout layout = mytextview.getLayout();一直是null。后来我发现了更好的办法:
首先自定义一个TextView的子类,声明两个个方法:
1
2
3
4
5
6
7
8
9
10
11
|
private
int
getAvailableWidth()
{
return
getWidth() - getPaddingLeft() - getPaddingRight();
}
private
boolean
isOverFlowed()
{
Paint paint = getPaint();
float
width = paint.measureText(getText().toString());
if
(width > getAvailableWidth())
return
true
;
return
false
;
}
|
然后在onTextChanged里调用isOverFlowed就行了。
http://www.hankcs.com/program/mobiledev/android-textview-determine-whether-excess-of-ellipsis.html