StaticLayout getEllipsisCount返回0,当我知道这是ellipsizing? [英] StaticLayout getEllipsisCount returning 0 when I know it's ellipsizing?

查看:1867
本文介绍了StaticLayout getEllipsisCount返回0,当我知道这是ellipsizing?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有一个扩展的TextView我试图从这样我可以检测,如果文本通过调用getEllipsisCount将关闭屏幕创建StaticLayout。

So I have an extended TextView that I'm trying to create a StaticLayout from so that I can detect if the text is going off-screen by calling getEllipsisCount.

因此​​,从TextView的在我构建了staticlayout像这样:

So from within the TextView I'm constructing the staticlayout like so:

layout = new StaticLayout(getText(), getPaint(), getWidth(), Alignment.ALIGN_NORMAL, 0f, 0f, false);

但是,即使layout.getLineCount()返回行的正确数目,getEllipsisCount(N)永远不会返回> 0,甚至当我可以清楚地看到它添加省略号。

But even though layout.getLineCount() returns the correct number of lines, getEllipsisCount(n) never returns > 0, even when I can clearly see it adding ellipsis.

所以我猜这已成为检测,如果文本已经关闭屏幕一个不很好的方式......所以没有人知道一个更合适的方式,或如何得到这个工作?

So I'm guessing this has become a not-good way to detect if text has gone off the screen... so does anyone know of a more appropriate way, or how to get this to work?

推荐答案

我也遇到了这个问题,在Android上4.2.2。我能避开它通过使用 ViewTreeObserver API设置回调的onGlobalLayout事件,并启动延迟可运行的有:

I also encountered this problem, on Android 4.2.2. I was able to get around it by using the ViewTreeObserver API to set a callback for the onGlobalLayout event and launching a delayed runnable from there:

ViewTreeObserver observer = _someView.getViewTreeObserver();    
observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() 
                @Override    
                public void onGlobalLayout() {
                  _somView.getViewTreeObserver().removeGlobalOnLayoutListener(this);    
                  _textView.postDelayed(new Runnable() {  
                       public void run() {
                          // Code that uses ellipsis detection here
                       }
                    }, 10); 
                });

这是无可否认有点hackish的,但它是我能得到正确检测省略号的唯一途径,否则它只是不停地报道,没有ellipsisification正在发生。在我的测试的延迟是必不可少的这个工作,但整个事情的执行速度不够快,以便在此基础上的信息,而用户界面闪烁的元素,可以隐藏/显示。

This is admittedly somewhat hackish, but it's the only way I could get the ellipsis to be correctly detected, otherwise it just kept reporting that no ellipsisification was taking place. In my testing the delay is essential for this to work, but the whole thing executes quickly enough so that elements can be hidden/shown based on this information without the ui flickering.

这篇关于StaticLayout getEllipsisCount返回0,当我知道这是ellipsizing?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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