Android, How to limit width of TextView (and add three dots at the end of text)?

前端 未结 21 1830
野的像风
野的像风 2020-12-02 03:44

I have a TextView that I want to limit characters of it. Actually, I can do this but the thing that I\'m looking for is how to add three dots (...) at the end o

相关标签:
21条回答
  • 2020-12-02 04:36

    I am using Horizonal Recyclerview.
    1) Here in CardView, TextView gets distorted vertically when using

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

    Check the bold TextViews Wyman Group, Jaskolski...

    2) But when I used singleLine along with ellipsize -

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

    Check the bold TextViews Wyman Group, Jaskolski...

    2nd solution worked for me properly (using singleLine). Also I have tested in OS version: 4.1 and above (till 8.0), it's working fine without any crashes.

    0 讨论(0)
  • 2020-12-02 04:36

    I think you give fix height and width of text view. Then your solution will work.

    0 讨论(0)
  • 2020-12-02 04:36

    you can do that by xml:

    <TextView 
        android:id="@+id/textview"
        android:maxLines="1" // or any number of lines you want
        android:ellipsize="end"
        />
    
    0 讨论(0)
  • 2020-12-02 04:36

    Simple for three dots

    android:layout_width="100dp" <!--your dp or match_parent or 0dp>
    android:maxLines="2" <!--count your line>
    android:ellipsize="end"
    
    0 讨论(0)
  • 2020-12-02 04:38

    you can write this line in xml where you take the textview :

    android:singleLine="true"
    
    0 讨论(0)
  • 2020-12-02 04:38

    Apart from

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

    you should set

    android:layout_width="0dp"
    

    also know as "match constraint", because the wrap_content value just expands the box to fit the whole text, and the ellipsize property can't make its effect.

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