ellipsize_end textview


Most of the time, the text display is limited, there is a Maxine, and then you are required to intercept at the end of the last line, and the end of the last line is… (XXX text…), so just set the ellipsize in your XML_ Just end,howeverSometimes the demand is strange, system controls can not meet the requirements of the product

For example, the effect is shown in the figure below

The textview should leave some space at the end, because there is an index at the end of the line. At this time, the end mode of the system can not be realized. If you set the ellipsize of textview to end in XML, the end text will go directly to the far right, and the following index style will be overlapped


Then you may think of a common control effect, folding textview. The text is too long, and all the text will be displayed at the end. You will collapse the extra text. Then you wonder if you can achieve this effect by setting all the text as spaces. Of course, you can see if the general source code implementation is feasible. At that time, in order to be lazy, I actually thought so,

When I look at the code, it feels feasible, because I see an attribute   Layout, and there is a method in layout,   GetLineCount (), this method can get the number of lines of text, I know all the lines, so how to play is not has the final say, so it was opened, everything is perfect, the effect is nice, but the latter is too short, it will still block the index, and then to lazy.

With the initial idea, plus the blank, it is all invisible. It is not the same as the result. It is not has the final say, so it will imitate the effect of folding textview, and turn it into blank.

Problems arise

The space is added, and the effect is generally perfect, but there is a special case. When the text almost reaches the end mode, it is assumed that your maxlie is 2, but the title text is only two lines short of a few characters. At this time, the ellipsize effect can not be triggered, because getlinecount() returns 2 and will not enter the end mode, Then I open my mind and continue to continue the previous thinking. The blank space is big. I leave a few blank spaces in the text, but I can’t give it up soon. Anyway, the end is blank, which doesn’t affect it. So I add the blank space. At this time, the real problem comes

Dead end

Generally, there is one. However, after self-test, generally, there is no problem. However, if there is a space at the end of the text, then the end mode can’t be triggered, and then I think it is the text space that hasn’t been calculated. When I hit a breakpoint, I found that there are many spaces at the end of the text. At that time, I tried several times to add spaces all the time, Because I was worried that the space was not enough to trigger, and a line of space was added after that, so I went to the system method to check. Later, I found the problem. The original getlinecount() method returned 2. The length of my text obviously exceeded this length, but getlinecount() did not calculate the length of the space at the end, so it did not work, Then I began to study why getlinecount() returned 2 instead of 3. After checking a lot of data, it didn’t work. As long as it was a space, I wouldn’t calculate. Then I thought about whether to add a string of text directly after it, and then set that part of the text to a transparent color. The more I thought about it, the more outrageous it was

Wake up, change your mind

So I asked the boss in the next group, and the boss said, I don’t care if you have any problems with getlinecount (), first of all, what effect do you want to achieve, and then I honestly told him, I want this, and that, and then the boss said, is it so difficult to measure by yourself? Sometimes it’s hard to get out of that mode. Later, I decided to abandon the previous idea, instead of getlinecount (), and measure the text by myself. That’s not a very simple thing. Why do I have to use getlinecount (), and there are so many operations, and then I directly customize a simple textview, Directly rewrite the settext method, measure, and then directly substring, which is simple and convenient

Getlinecount() is specially annotated above, and then measured step by step to get the information you want. At the end, because you want to leave the index blank, you can write – 6, which can be configured by yourself. Here, it’s relatively simple, and it’s fixed. Run it

But young man, do you think it’s over?

At this point, you will also find that the measurement is asynchronous after initialization. When refreshing, you will find that it will flicker. Because you have seen the method of folding textview and other controls before, you know that you need to add a flag. After initialization, you don’t need to listen again

It’s perfect at this time, but young, do you think it’s over?

Now I come back to the problem mentioned above. If my content is just a few characters short of the condition, the measured line is still 2, and it will not enter the end mode, but getlinecount () is no longer needed at this time, because line is measured by itself, and of course there is no need to make a big fuss about the space. I can measure more than two lines here, Then I can also calculate whether it is beyond 1.7 lines, 1.5 lines, as long as you feel, I dare to do it

Add a judgment. Even if you are short of a few characters, I can still let you enter the end mode. You will find that it’s easy to expand at this time. The thing is different. Ha, in run, it’s perfect. You will find that the title has become a new one


You can control the white space in the back, and finally take a look at the rendering

Use the same as native, because it’s an overridden settext

Recommended Today

Looking for frustration 1.0

I believe you have a basic understanding of trust in yesterday’s article. Today we will give a complete introduction to trust. Why choose rust It’s a language that gives everyone the ability to build reliable and efficient software. You can’t write unsafe code here (unsafe block is not in the scope of discussion). Most of […]