Text does not ellipsize

前端 未结 13 1348
隐瞒了意图╮
隐瞒了意图╮ 2020-12-25 11:43

I have a TextView set to ellipsize but when I put the text in it doesn\'t do so. Any ideas?



        
相关标签:
13条回答
  • 2020-12-25 12:09

    Only works runtime, do something like this:

    textView.setEllipsize(TruncateAt.END);
    textView.setLines(1);
    textView.setHorizontallyScrolling(true);
    textView.setText("Long text goes here");
    
    0 讨论(0)
  • 2020-12-25 12:10

    You can as well set maxLines like so:

            <TextView
                android:id="@+id/txt_title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:maxLines="3"
                android:ellipsize="end"
                android:textStyle="bold"/>
    

    Ellipses appear when the text exceeds the maxLines set.

    0 讨论(0)
  • 2020-12-25 12:11

    If you are trying to get multiline ellipsis working, unfortunately it doesn't work very well. This is a known bug in the Android platform and as far as I know it hasn't been fixed.
    In a TextView the best you can get is 2 lines ellipsizing (which can be achieved by setting

    android:maxLines="2"
    

    Refer to this link.
    Even with an absolute width as mentioned in another answer, it still only gives you at max, 2 lines of ellipsis. (But as mentioned also, single line ellipsis is achievable). So for example, your text might fill your 8 line TextView, but will look like this when you turn ellipsis on:

    +---------------------------------------------------+
    |                                                   |
    | This is my cool text that is supposed to fill the |
    | entire textview but unfortunately because of t... |
    |                                                   |
    |                                                   |
    |                                                   |
    |                                                   |
    |                                                   |
    |                                                   |
    |                                                   |
    |                                                   |
    |                                                   |
    |                                                   |
    |                                                   |
    |                                                   |
    |                                                   |
    +---------------------------------------------------+
    
    

    If you know the size of your TextView, you can use a custom component available here (Unfortunately the google code project that originally hosted it seems to have disappeared, hence this link is all I could find).

    0 讨论(0)
  • 2020-12-25 12:15
    android:layout_width="wrap_content"
    

    will allow the TextView to expand as long as it needs to (including running off the screen). To make it ellipsize, you're going to have to set a width as citizen conn recommended, preferably with android:layout_width="fill_parent" instead of an absolute value.

    Additional hints: You'll also want to set the maxLines of the TextView (probably to 1), and to get the actual ellipsis ("...") to appear, you'll probably also have to set

    android:scrollHorizontally="true"
    
    0 讨论(0)
  • 2020-12-25 12:17

    I think you need to specify an absolute width for it to work.

    android:layout_width="100dip"
    

    or

    android:layout_width="fill_parent"
    
    0 讨论(0)
  • 2020-12-25 12:18

    android:maxWidth="150dp" was the only instance when text ellipsized for me.

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