android append '…' at the end textview _ED

2019-07-27 10:49发布

I have a lsit view and i that i need to add some text. in the adapter, I need to append ... at the end and i used the following when i give

android:ellipsize="end"

in the only shows 2 line eg:
asdnfsdfdsf asdfsdfasdf
sdfsd sdfsdf sdfsd ad...

And when i give

android:ellipsize="start"

it is as :
asdnfsdfdsf asdfsdfasdfd
...sdfsd sdfsdf sdfsd ad

The complete code i used:

<TextView  android:layout_width="wrap_content" android:background="#016A7D"

   android:layout_height="80dp" android:textColor="#000000"

    android:maxLines="4"          

    android:id="@+id/list_report_desciption" 

    android:ellipsize="end"

/> And the output i got is:

What exactly will a smarter planet
look like? How's IT changing? A...

actually it contains more than 6 lines
Is there any thing i need to set so that i need to get 3 lines
Thanks

4条回答
女痞
2楼-- · 2019-07-27 11:39

Nothing worked for me:-(
I just fixed the length. Cut the string in that length and append '...' after that.
Now its working for me.

查看更多
你好瞎i
3楼-- · 2019-07-27 11:45

Text ellipsization seems buggy, accroding to this report: http://code.google.com/p/android/issues/detail?id=10554

A workaround allows ellipsizing one line of text, by setting the android:singleLine attribute to true. But this attribute is deprecated.

查看更多
叼着烟拽天下
4楼-- · 2019-07-27 11:45

The solution you accepted will not scale correctly on all devices.

Reason : the width of "a" is lesser than "A", so, if you are truncating string based on some size (say 15 letters) and adding "..." through code. You might see results not expected.

Now coming to the solution:-

Solutions:

Solution # 1 : Add the following 3 attributes to your TextView and you'll get the result you want :)

<TextView
    android:ellipsize="end"
    android:lines="1"
    android:scrollHorizontally="true" />

Solution # 2 : Another workaround might be, you could decide to marquee text (the fancy animation moving your text from right to left). For that you need following attributes in your TextView xml:-

<TextView
    android:id="@+id/my_text_view"
    android:ellipsize="marquee"
    android:lines="1"
    android:scrollHorizontally="true"
    android:marqueeRepeatLimit="marquee_forever" />

And then in you code you need to get the TextView by its id and put-in the following line:-

TextView myTextView = (TextView) findViewById(R.id.my_text_view);
myTextView.setSelected(true);

#### Edit:- ####

I just figured out that for my solution to work in android versions greater than 2.3.x we need to add the following line in our TextView xml :-

    android:singleLine="true"

Although its a deprecated attribute, but you have to add this, otherwise marquee or "..." wont work.

Hope this answers the question :)

查看更多
孤傲高冷的网名
5楼-- · 2019-07-27 11:49

I was looking for a simple solution for this myself, I ended up with something like:

private String getTextWithMoreIndicator(TextView textView, String text) {

    long maxVisibleChars = textView.getPaint().breakText(text, true, textView.getMeasuredWidth(), null);

    if(maxVisibleChars < text.length()) {
        text = text.substring(0, (int)maxVisibleChars -3) + "..."; 
    }

    return text;
}
查看更多
登录 后发表回答