android如何获取textview最多显示

今天在公司遇到一个需求;TextView设置最多显示8个字符,超过部分显示...(省略号),那么android如何获取textview最多显示,感兴趣的可以了解一下

方法一

工作中用的一个方法,虽然不算特别准确,但效果还是不错的,这里分享下。

 /** * 获取textview最大能显示几个字 * @param text 文本内容 * @param size 文本字体大小 * @param maxWidth textview的最大宽度 * @return */ private float getLineMaxNumber(String text, float size,float maxWidth) { if (null == text || "".equals(text)){ return 0; } Paint paint = new Paint(); paint.setTextSize(size); //得到文本内容总体长度 float textWidth = paint.measureText(text); // textWidth float width = textWidth / text.length(); float total = maxWidth / width; return total; }

上面这个方法不太精确,不过比较适合在 RecyclerView 或 ListView 里面使用,避免生成太多对象

方法二

 /** * 获取textview一行最大能显示几个字(需要在TextView测量完成之后) * * @param text     文本内容 * @param paint    textview.getPaint() * @param maxWidth textview.getMaxWidth()/或者是指定的数值,如200dp */ private int getLineMaxNumber(String text, TextPaint paint, int maxWidth) { if (null == text || "".equals(text)) { return 0; } StaticLayout staticLayout = new StaticLayout(text, paint, maxWidth, Layout.Alignment.ALIGN_NORMAL , 1.0f, 0, false); //获取第一行最后显示的字符下标 return staticLayout.getLineEnd(0); } 

利用 StaticLayout 可以非常轻松的得到一行可以显示的最大字符数

延伸:
对于一个单行 TextView,当字符串超出一行时,如何获取未显示的部分字符串?
textview 设定最大行数为 1 后,文本超出了 textview,textView 末尾显示省略号,我就想知道省略号代表的内容
思路:
假设 TextView 的宽度是在 xml 内设置的具体数值,比如 300dp,
(目的是为了简化这个问题,如果设置为 match_parent 或者 wrap_content,需要在程序运行时计算其宽度,而直接 getWidth 总是返回 0,比较麻烦。)
比如是这样配置的:

 

然后填充了一个超长的字符串,比如这样:

 String str = "If you really want to hear about it, the first thing you'll probably want to know"; 这样就会导致显示不全,像这样: If you really want to hear about it, the first thin... 所以,如果你想得到已显示的字符个数,或者未显示的字符个数,那么其中的关键是如何计算每一个字符的宽度。 然后遍历这个字符串,当前n个字符宽度总和,超过TextView宽度时,就得到了已显示的字符个数。 String str = "If you really want to hear about it, the first thing you'll probably want to know"; mTextView = (TextView) findViewById(R.id.textView); // 计算TextView宽度:xml中定义的宽度300dp,转换成px float textViewWidth = convertDpToPixel(300); float dotWidth = getCharWidth(mTextView, '.'); Log.d(TAG, "TextView width " + textViewWidth); int sumWidth = 0; for (int index=0; index

结果如下,在荣耀 3C 和 LG G3 上测试通过(G3 比计算的结果,多显示了一个字符):

10-22 01:17:42.046: D/Text(21495): TextView width 600.0
10-22 01:17:42.048: D/Text(21495): #0: I, width=8.0, sum=8
10-22 01:17:42.049: D/Text(21495): #1: f, width=9.0, sum=17
10-22 01:17:42.049: D/Text(21495): #2:  , width=7.0, sum=24
10-22 01:17:42.049: D/Text(21495): #3: y, width=14.0, sum=38
......
10-22 01:17:42.053: D/Text(21495): #17: t, width=9.0, sum=213
10-22 01:17:42.053: D/Text(21495): #18:  , width=7.0, sum=220
10-22 01:17:42.053: D/Text(21495): #19: t, width=9.0, sum=229
......

10-22 01:17:42.061: D/Text(21495): #50: n, width=16.0, sum=575
10-22 01:17:42.061: D/Text(21495): #51: g, width=16.0, sum=591
10-22 01:17:42.061: D/Text(21495): TextView shows #51 char: If you really want to hear about it, the first thin

以上就是android如何获取textview最多显示的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » 移动