TextView is set to display up to the specified number of characters, and more than part is displayed... (ellipsis)

Today, I met a requirement in the company; the TextView is set to display up to 8 characters, and the excess part is displayed... (ellipsis). I found a lot of information on the Internet. Some people say that the TextView's android:signature="true" is set separately, and the android: ellipsize="end"; but I tried it and it didn't work. Finally, I tried the following method: for your reference

 

[java]  view plain copy View code snippets on CODE Derive to my code slice
 
  1. <TextView   
  2. android:id="@+id/tv"  
  3. android:layout_width="wrap_content"  
  4. android:layout_height="wrap_content"  
  5. android:maxEms="18"  
  6. android:singleLine="true"  
  7. android:ellipsize="end"  
  8. />  

Here we don't talk about those complex attributes, just say that we usually use more, android: maxlength

The official website API explains it as:


 

The first sentence, that is to say, he is an inputfilter (input filter), his role is to limit the text, length (text length) through specified, number (the number you specify), here specifically refers to the length of the text, regardless of What content do we input, English, symbols, numbers, Chinese characters... These all belong to the text range, so the role of maxlength is to directly limit the length of the display,

for example:


 

The interface effect is:


    7 letters + 3 Chinese characters.

今天做到这里的时候发现不能单纯利用这个属性做限制,就看了别的,发现还有个maxEms的属性,官网对其的解释为:


 

第一句:翻译过来是使得textview的宽度不少于很多ems的宽度

What???????此时超级不理解ems是啥?反正绝对不能是邮政快递吧,很迷茫在网上查了下各种开发者的解释,如下:


 

这位说是限制字符数量,那么我的理解就是严格按照中文两个字符长度,字母占一个这样的规则,于是写了个例子进行测试,


 

界面结果是:


   的确是自动换行了,但是第一行显示了7个字母加2个汉字,按照字节数来说的话应该是7+4=11才对啊,完全不是五个,但是往下看,第二行,是四个汉字加一个中文标点,10个字符长度,5个字符,莫非是这个意思?

然后我进行了下面几种测试:



 

  第一行是10个a,我就想难道在这里,中文算1个字符长度,字母算半个?

继续测试



 

第一行显示的是25个字符(包含三个英文标点),阿西吧,刚刚建立的世界观就毁了,看来也不是这种计算方式。

继续测++:



 

    到这里的时候我就欢乐了,应该就是指中文汉字数目吧,继续验证



 

   我了个去…………第一行咋又出现了11个字呢………………

于是我从1开始验证到了18,发现如下不成规律的规律:

 

设你输入的参数为n, 屏幕显示的数量为m。

值为1-5时,m = n..

6-11时,m = n+1。

12-18时,m = n+2。

再大我就没测试了,因为屏幕放不下了……………………

基本是符合规律的,对于结果+1和+2,我的猜想是,汉字间是有间隙的,汉字一多,加上间隙,宽度就会增大。也只是猜想,再反过来看看API。


 

虽然还是不明白ems是什么,但是这里有个很重要的词,wide,宽度范围,而没有强调长度,那么我想应该就是字符在屏幕中所占的宽度了,既然这样,就和屏幕宽度和字体大小有直接的关系。

       本文到此基本能结束了,相信大家也有点了解了,虽然我也不能确定自己的猜想是正确的,但是拿来用应该没问题了,但是我对这个方法还有点吐槽:

1, GoogleAPI会以汉字为基准吗?地位一下提高的感觉啊……

2. For the English symbol of ems, it has no meaning, how should we guess.

3. Under the condition of android, various resolutions, various screen standards, and the cottage phone also likes to tamper with the default font, it is not very suitable to use this spatial layout attribute... There are greater limitations.

4. To sum up, this property and method is not easy to use...over.

I don't know if my verification and understanding are right or not, or if I understand it wrong at all. I hope that the great gods who have a more reliable understanding of this will give me a lot of pointers, and I would like to thank them here.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324921359&siteId=291194637