Android TextView 和获取文本行
我有一个 TextView,其文本可以运行多行。创建并动态设置后,我想
- 获取给定行上的文本,并
- 了解该行的宽度和高度。
我该怎么做?
I have a TextView whose text can run to many lines. Once it has been created and set dynamically, I'd like to
- Get the text on a given line, and
- Know the width and height of this line.
How do I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我理解正确的话,问题 2 的答案是:
以像素为单位的宽度应该是
abs(bounds.right -bounds.left)
;高度是abs(bounds.bottom -bounds.top)
你的第一个问题有点棘手,但这样的事情应该可以实现所需的魔力:
If I understand correctly the answer to question 2 is:
The width in pixels should be
abs(bounds.right - bounds.left)
; and the height isabs(bounds.bottom - bounds.top)
Your first question is a bit more tricky, but something like this should do the required magic:
对于第一个问题:
之前选择的答案没有适合我,因为我的文本视图有自动换行。
使用以下行从给定行号“i”获取文本
Kotlin:
如果缺少解释,请告诉我。
PS:我不是安卓高手。只是分享我的知识以防有人觉得有用
For the First question:
The previous selected answer didn't make the cut for me as my textview had word wrap.
Use the following line to get the text from the given line number 'i'
Kotlin:
Let me know if it is lacking explanation.
PS: I'm no master in android. Just sharing my knowledge in case someone finds it useful