Is there a way to check if a TextView's text is truncated

前端 未结 3 1333
迷失自我
迷失自我 2020-12-18 23:38

Is there a way to check whether a TextView\'s text is truncated in my code?

相关标签:
3条回答
  • 2020-12-18 23:49

    You can use method: getEllipsisCount

     public static boolean isTextTruncated( String text, TextView textView )
        {
            if ( textView != null && text != null )
            {
    
                Layout layout = textView.getLayout();
                if ( layout != null )
                {
                    int lines = layout.getLineCount();
                    if ( lines > 0 )
                    {
                        int ellipsisCount = layout.getEllipsisCount( lines - 1 );
                        if ( ellipsisCount > 0 )
                        {
                            return true;
                        }
                    }
                }
    
            }
            return false;
        }
    

    I found the answer at the following related post:

    Check if textview is ellipsized in android

    0 讨论(0)
  • 2020-12-18 23:57

    Don't think so but you can read here to properly handle it if you only want the text to use one line.

    0 讨论(0)
  • 2020-12-19 00:02

    Check out this topic

    ... You can create a Paint object with TextView2's text size and use breakText() to measure how many characters will fit in your TextView2's width...

    It works for me

    0 讨论(0)
提交回复
热议问题