Strange issue with android:ellipsize=“end”

前端 未结 5 2162
野的像风
野的像风 2021-02-20 14:53

I am using android:ellipsize=\"end\" in android xml file, & surprisingly I am not getting the layout that I want, the 3 dots(...) are showing but after that

相关标签:
5条回答
  • 2021-02-20 15:19

    When setting the text for the textview make sure the text is not greater than 750 characters so use txt = theText.substring(0, 750) or something like that before calling settext. This works on the emulator with your feed. and should be enough characters for a 10inch tablet too

    0 讨论(0)
  • 2021-02-20 15:27

    Take a look at this post. There is a custom EllipsizingTextView which solves ellipsizing problem for multiline view. However it may solve you problem too.

    0 讨论(0)
  • 2021-02-20 15:28

    i used :

    if (myModel.mDataText().length() > 150) {
        mTextView.setText(Html.fromHtml(myModel.mDataText().substring(0, 150) + "..."));
    } else {
        mTextView.setText(Html.fromHtml(myModel.mDataText() + "..."));
    }
    
    0 讨论(0)
  • 2021-02-20 15:33

    Finally found what was causing this issue in my app!!

    While RuAware's answer led me on the right path, I found that it didn't fix the problem for all of my text blocks. Given that it worked for some, I felt it was clearly due to some malicious character. So I went through one broken text block and just started removing characters until it stopped breaking.

    It turns out it was the new line character '\n' that was causing it. Even if the new line was after the ellipsize, the new line character was [inconsistently] causing this problem as long as it was somewhere within the string.

    The issue is solved by doing something similar to the following before setting the text to your view:

    text = text.replace('\n',' ');
    
    0 讨论(0)
  • 2021-02-20 15:34

    I was experiencing the same problem when displaying a String containing html formatted text.

    Removing all html tags with myText.replaceAll("\\<.*?>","") solved the problem.

    You can also remove html tags using Html.fromHtml(myText).toString(), but notice that you must use .toString() and not apply the fromHtml output directly to the TextView. If some special characters are html encoded (&amp, &gt, &lt...) you can safely apply again Html.fromHtml() to the final String.

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