获取Textview中的一行可以包含多少个字符 [英] Get how many characters a line in Textview can have

查看:151
本文介绍了获取Textview中的一行可以包含多少个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有一个很大的String和许多textview,它们都具有maxLines="1"layout_width="match_parent"以及等宽字体.

Let's say we have a large String and many textviews which all have maxLines="1" and layout_width="match_parent"and a monospaced font.

在需要换行之前,我需要知道可以在字符串中放入多少个字符.

I need to know how many characters of my string I can put in a Textview before it needs a new line.

如果该解决方案也可以与EditText一起使用,那就太好了.

It would be great if the solution could work with EditText too.

推荐答案

TextPaint paint = textView.getPaint();
int wordwidth=(int)paint.measureText("a",0,1);
int screenwidth = getResources().getDisplayMetrics().widthPixels;
int num = screenwidth/wordwidth

这篇关于获取Textview中的一行可以包含多少个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆