如何获取文本的最大行数
如何获取TextView最多可写入的行数?我尝试了这个:
DisplayMetrics metrics = new DisplayMetrics ();
getWindowManager (). getDefaultDisplay (). getMetrics (metrics);
float width = metrics.widthPixels;
float height = metrics.heightPixels;
int lines = (int) (height / textView.getLineHeight ());
System.out.println (lines);
但是,这个变体提供了错误的信息。
How to get the maximum number of lines that can be written in TextView? I tried this:
DisplayMetrics metrics = new DisplayMetrics ();
getWindowManager (). getDefaultDisplay (). getMetrics (metrics);
float width = metrics.widthPixels;
float height = metrics.heightPixels;
int lines = (int) (height / textView.getLineHeight ());
System.out.println (lines);
However, this variant gives the wrong information.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
全部在 文档
编辑:我有点太快了。我假设您想要文本视图中可以包含的行数,而不是文本视图中当前有多少行......在这种情况下 ->看看这个问题: 根据宽度计算文本大小文本区域
It's all in the documentation
EDIT: I was a bit too fast. I assume you want the number of lines that CAN be inside a textview instead of how many there currently ARE in the textview... In that case -> have a look at this question: Calculate text size according to width of text area
textview.getLineCount() 会帮助你
textview.getLineCount() will help you
textView.getHeight()
和textView.getLineHeight()
都会根据屏幕 dpi 考虑屏幕高度。这是获得正确视图高度的帖子。
视图的 getWidth() 和 getHeight() 返回 0
textView.getHeight()
andtextView.getLineHeight()
both will consider the height on the screen with the screen dpi.Here is the post to get the correct height of view.
View's getWidth() and getHeight() returns 0