Scrolling a TextView to a specific line

前端 未结 1 1096
傲寒
傲寒 2020-12-10 14:41

I have a TextView inside a ScrollView. Let\'s say the ScrollView is named s and the TextView is named t.

I have many lines to be displayed

相关标签:
1条回答
  • 2020-12-10 15:20

    Found the answer here.

    Instead of calling scrollTo directly, we must call post instead on the ScrollView. This works.

    t.setText(aVeryLongString);
    s.post(new Runnable() {
        @Override
        public void run() {
            int y = t.getLayout().getLineTop(40); // e.g. I want to scroll to line 40
            s.scrollTo(0, y);
        }
    });
    
    0 讨论(0)
提交回复
热议问题