以编程方式滚动文字眼帘的一个TextView眼帘、文字、方式、TextView

2023-09-04 12:07:59 作者:小奶豪

我的TextView有大量的文本,对于一些用户搜索,我想强调的比赛,但他们可能是一路下滑的页面,有没有办法来滚动到第一场比赛?

我找遍了谷歌和似乎并没有找到相关的任何东西。

我的布局:

 <滚动型机器人:layout_width =match_parent
            机器人:layout_height =0dp
            机器人:layout_weight =1
            机器人:requiresFadingEdge =垂直
            机器人:ID =@ + ID / sclView>

    < TextView的Andr​​oid的:layout_width =match_parent
              机器人:layout_height =WRAP_CONTENT
              机器人:ID =@ + ID / txtView
              机器人:TEXTSIZE =16SP
              机器人:paddingTop =10dp
              机器人:以下属性来=20dp
              机器人:paddingRight =20dp
              机器人:paddingBottom会=10dp
              机器人:lineSpacingExtra =5DP
              机器人:文字颜色=@色/ TextGray
              机器人:textIsSelectable =真/>
< /滚动型>
 

解决方案

我假设有没有水平滚动。如果有,则可以推断,从这个答案。看到你正在凸显的比赛,我也要去假设你有发现部分下来了。这意味着你应该至少在搜索字符串的CharSequence

的起始位置

使用 Layout.getLineForOffset 以找到行的字符串,然后的 Layout.getLineTop 获得该行的顶部的位置,然后的 View.scrollTo 以滚动到所需的位置。它应该是这个样子:

 的Layout布局= textView.getLayout();
scrollView.scrollTo(0,layout.getLineTop(layout.getLineForOffset(startPos)));
 
android textview 动画,带滚动动画的 Android TextView RollingTextView

我还没有尝试过这一点,所以你可能必须做一些事情来处理,你已经添加填充等。但我认为,总体思路应该工作。

I have TextView with a large amount of text, the user searches for something and I highlight the matches, however they may be way down the page, is there a way to scroll to the first match?

I looked all over google and did not seem to find anything relevant.

My Layout :

<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>

解决方案

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.

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.

 
精彩推荐