How can I show ellipses on my TextView if it is greater than the 1 line?

后端 未结 7 1217
终归单人心
终归单人心 2020-11-30 22:19

I have the following Layout which does not work:



        
相关标签:
7条回答
  • 2020-11-30 22:34

    This is a common problem. Try using the following:

    android:scrollHorizontally="true"
    android:ellipsize="end" 
    android:maxLines="1"
    

    .............. the scrollHorizontally is the "special sauce" that makes it work.

    0 讨论(0)
  • 2020-11-30 22:36

    The way it worked for me on multiple devices / APIs was programmatically like this (where tv is your TextView):

        if (tv.getLineCount() > 1) {
            int lineEndIndex = tv.getLayout().getLineEnd(0);
            String text = tv.getText().subSequence(0, lineEndIndex - 3) + "\u2026";
            tv.setText(text);
        }
    
    0 讨论(0)
  • 2020-11-30 22:37

    So all the answers above cater to the requirement that only 1 line and then the ellipsis should appear. However if you want the ellipsis to appear after certain lines of text, then you should use the following:

    android:ellipsize="end"
    android:maxLines="2"
    android:singleLine="false"
    

    With this the ellipsis will appear only after 2 lines. Note: Its important to have singleLine as false.

    0 讨论(0)
  • 2020-11-30 22:44

    Use this

    android:ellipsize="end"  
    android:singleLine="true"
    

    Don't use this without fully aware of what output comes

    android:ellipsize="end"  
    android:maxLines="1"
    

    When you use maxlines = 1 it will some time truncate most of the characters.

    0 讨论(0)
  • 2020-11-30 22:45

    This helped me out:

    android:ellipsize="end"
    android:maxLines="1"
    android:singleLine="true"
    

    Make sure the TextView width is set to Match_Parent

    https://github.com/chrisjenx/Calligraphy/issues/43#issuecomment-523701518

    0 讨论(0)
  • 2020-11-30 22:48

    This will also make a single line with ellipsise

     android:singleLine="true"
    
    0 讨论(0)
提交回复
热议问题