我有一个包含大量文本的TextView,用户搜索某些内容后,我会高亮匹配项,但这些匹配项可能在页面底部,有没有一种方法可以滚动到第一个匹配项处?
我已经在Google上搜索过很多,似乎没有找到相关的内容。
我的布局:
<ScrollView android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:requiresFadingEdge="vertical"
android:id="@+id/sclView">
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txtView"
android:textSize="16sp"
android:paddingTop="10dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingBottom="10dp"
android:lineSpacingExtra="5dp"
android:textColor="@color/TextGray"
android:textIsSelectable="true"/>
</ScrollView>
scrollView.scrollTo(0, layout.getLineTop(txtView.getTop() + layout.getLineForOffset(startPos)));
这样的操作。因此,txtView.getTop()获取textView相对于其父视图的顶部位置(以像素为单位),并将其添加到您要查找的行的getLineTop垂直位置。 - nommer