我如何知道我的TextView已经ellipsized? [英] How do I tell if my textview has been ellipsized?
问题描述
我有一个多行的TextView
,有安卓ellipsize =结束
集。我想不过要知道,如果字符串我在那里的地方实际上是太长(这样我可以确保全串在其他地方显示在页面上)。
I have a multi-line TextView
that has android:ellipsize="end"
set. I would like to know, however, if the string I place in there is actually too long (so that I may make sure the full string is shown elsewhere on the page).
我可以使用 TextView.length()
并找到什么字符串的近似长度适合,但因为它是多线,的TextView
处理时,包装,所以这并不总是可行的。
I could use TextView.length()
and find about what the approximate length of string will fit, but since it's multiple lines, the TextView
handles when to wrap, so this won't always work.
任何想法?
推荐答案
您可以得到的TextView
的布局,并检查每行的省略号计数。对于最终省略号,就足够了检查的最后一行,像这样的:
You can get the layout of the TextView
and check the ellipsis count per line. For an end ellipsis, it is sufficient to check the last line, like this:
Layout l = textview.getLayout();
if (l != null) {
int lines = l.getLineCount();
if (lines > 0)
if (l.getEllipsisCount(lines-1) > 0)
Log.d(TAG, "Text is ellipsized");
}
在布局阶段之后,这仅适用,否则返回布局为空,所以把这个在你的code一个合适的地方。
This only works after the layout phase, otherwise the returned layout will be null, so call this at an appropriate place in your code.
这篇关于我如何知道我的TextView已经ellipsized?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!