开发者

StaticLayout getEllipsisCount returning 0 when I know it's ellipsizing?

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.

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

layout = new StaticLayout(getText(), getPaint(), getWidth(), Alignment.ALIGN_NO开发者_StackOverflowRMAL, 0f, 0f, false);

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?


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

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜