Scroll TextView to text position

后端 未结 3 1368
灰色年华
灰色年华 2020-12-20 03:51

I want to scroll my TextView to make visible a specific position in the text. How can I do that? I tried bringPointIntoView (int offset) but without success.

Source

3条回答
  •  臣服心动
    2020-12-20 04:18

    For those who have the same problem, I finally made my own implementation of bringPointIntoView:

      public static void bringPointIntoView (TextView textView,
      ScrollView scrollView, int offset)
      {
        int line = textView.getLayout ().getLineForOffset (offset);
        int y = (int) ((line + 0.5) * textView.getLineHeight ());
        scrollView.smoothScrollTo (0, y - scrollView.getHeight () / 2);
      }
    

    Don't hesitate if you have a better solution.

提交回复
热议问题