且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

以编程方式将单词滚动到文本视图中的视图中

更新时间:2022-12-26 10:33:09

我假设没有水平滚动.如果有,你可以从这个答案中推断出来.当您突出显示匹配项时,我还假设您已经完成了查找部分.这意味着您应该在 CharSequence 中至少有一个搜索字符串的起始位置.

I'm assuming there's no horizontal scrolling. If there is, you can extrapolate from this answer. Seeing as you're highlighting the matches, I'm also going to assume you have the finding part down already. This means you should have at least a start position for the search string in the CharSequence.

使用 Layout.getLineForOffset 找到字符串所在的行,然后 Layout.getLineTop 获取行首的位置,然后 View.scrollTo 滚动到您想要的位置.它应该看起来像这样:

Use Layout.getLineForOffset to find the line for the string, then Layout.getLineTop to get the position of the top of the line, then View.scrollTo to scroll to your desired position. It should look something like this:

Layout layout = textView.getLayout();
scrollView.scrollTo(0, layout.getLineTop(layout.getLineForOffset(startPos)));

我还没有试过这个,所以你可能需要做一些事情来处理你添加的填充等.但我认为一般的想法应该可行.

I haven't tried this out, so you might have to do something to deal with the padding that you've added, etc. but I would think the general idea should work.