如何判断我的 textview 是否已被椭圆化? [英] How do I tell if my textview has been ellipsized?

查看:14
本文介绍了如何判断我的 textview 是否已被椭圆化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个设置了 android:ellipsize="end" 的多行 TextView.但是,我想知道我放在那里的字符串是否真的太长(以便我可以确保完整的字符串显示在页面的其他地方).

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");
}

这仅在布局阶段之后有效,否则返回的布局将为空,因此请在代码中的适当位置调用它.

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 是否已被椭圆化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆