如何获取文本的最大行数

发布于 2024-12-20 21:56:15 字数 357 浏览 2 评论 0原文

如何获取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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

只涨不跌 2024-12-27 21:56:15

textview.getLineCount()

全部在 文档

编辑:我有点太快了。我假设您想要文本视图中可以包含的行数,而不是文本视图中当前有多少行......在这种情况下 ->看看这个问题: 根据宽度计算文本大小文本区域

textview.getLineCount()

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

酒中人 2024-12-27 21:56:15

textview.getLineCount() 会帮助你

textview.getLineCount() will help you

╰◇生如夏花灿烂 2024-12-27 21:56:15
textView.getHeight() / textView.getLineHeight()

textView.getHeight()textView.getLineHeight() 都会根据屏幕 dpi 考虑屏幕高度。

这是获得正确视图高度的帖子。
视图的 getWidth() 和 getHeight() 返回 0

textView.getHeight() / textView.getLineHeight()

textView.getHeight() and textView.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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文