如何将 ScrollView 滚动到文本中的某个位置 [英] How to scroll ScrollView to certain position in the text
问题描述
我有一个带有 TextView 的 ScrollView,我想将它滚动到某个段落,就像 HTML 中的锚点(例如 page.html#paragraph_id).
I have a ScrollView with a TextView inside it, and I'd like to scroll it to a certain paragraph, just like anchors in HTML (e.g page.html#paragraph_id).
有人知道怎么做吗?
谢谢!
推荐答案
确实是一个非常有趣的问题.我也很想这样做.
我所做的是将文本视图 - 行映射到滚动视图坐标的想法.
您可以做的是获取设备显示大小 - 以像素为单位.
和 textview 的行高 by- textview.getLineheight();并计算线坐标并将其映射到滚动视图
正如我所说,我也在努力做到这一点.我目前正在做同样的工作.如果我发现一些代码有效,我会通知你.
Really a very intresting question. I am also tyring to do so.
What I have done is an idea to map textview - lines to scrollview coordinates.
what you can do is get the device-display size - in pixels.
and a line height of the textview by- textview.getLineheight(); and calculate the line
coordinates and map it to the scrollview
As I said I am also trying to do this. I am currently working on the same.
If I find some code working I will let you know.
要做很多工作,因为 android os 没有提供您(或我)想要的这种滚动
或者您可以为每一行设置 textview 并使用此代码 mScrollView.scrollTo(0, your_text_view_with_the_line.getBottom());
Its lot of work to do because android os does not provide such kind of scroll you (or I) want
OR you can have textview for each line and use this code
mScrollView.scrollTo(0, your_text_view_with_the_line.getBottom());
这篇关于如何将 ScrollView 滚动到文本中的某个位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!