开发者

Text does not ellipsize

开发者_开发百科I have a TextView set to ellipsize but when I put the text in it doesn't do so. Any ideas?

<TextView 
    android:id="@+id/LegalsView" 
    android:ellipsize="end"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium" />


Try:

android:singleLine="true"


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"


You need to match the settings for android can correctly calculate the position of the object. Has several possible combinations, and depends on the rest of the layout is on the same line.

This is a combination that works:

android:layout_width="0dip" // dynamic width
android:layout_weight="1"   // less weight, other objects still "0"
android:ellipsize="end"     // ... into end of text (right)
android:singleLine="true"   // one line, deprecated, but necessary for some ambients
android:maxLines="1"        // one line, new mode

In summary, you indicate the weight, if you use dynamic layout, or just a fixed size, then indicates to only have one line and configures how "..." is displayed.


My app started ignoring maxlines & ellipsize when I added

android:textIsSelectable="true"

It seems like these options are not compatible together.


The only way I've been able to make this work is to set width="1dp", weight="1", ellipsize="end", maxLines="1" and scrollHorizontally="true". The first two have the same sort of effect as width="match_parent" without the widget expanding off the edge of the display, the remaining three settings all had to be made to get the text to ellipsize.


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).


If you aren't defining a specific width for your TextView the text will ellipsize first when your text is reaching the parent view's width.


This solution works:

android:ellipsize="end"
android:singleLine="false"
android:lines="3"
android:maxLines="3"

Width can also be set to "wrap_content"


You change your layout_width to certain dp instead of wrap_content

and

use
android:singleLine="true"

android:ellipsize="end"

code:

    android:layout_width="50dp"
    android:layout_height="wrap_content"
    android:text="check this text"
    android:id="@+id/textView"
    android:singleLine="true"
    android:ellipsize="end"/>


Only works runtime, do something like this:

textView.setEllipsize(TruncateAt.END);
textView.setLines(1);
textView.setHorizontallyScrolling(true);
textView.setText("Long text goes here");


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.


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


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

android:layout_width="100dip"

or

android:layout_width="fill_parent"
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜