屏幕旋转后如何恢复textview滚动位置?
在我的 Android 布局中,我有一个 TextView。该 TextView 显示相当大的可跨越文本并且能够滚动。现在,当手机旋转时,视图被销毁并创建,我必须再次 setText() TextView,将滚动位置重置到开头。
我知道我可以使用 getScrollly() 和scrollTo() 滚动到像素位置,但由于视图宽度的变化,行变得更长,并且像素位置 400 的行现在可能位于 250。所以这不是很有帮助。
我需要一种方法在 onDestroy() 中找到 TextView 中的第一条可见行,然后需要一种方法使 TextView 在旋转后滚动到这段特定的文本。
有什么想法吗?
In my Android Layout, I have a TextView. This TextView is displaying a rather large spannable text and it is able to scroll. Now when the phone is rotated, the View is destroyed and created and I have to setText() the TextView again, resetting the scroll position to the beginning.
I know I can use getScrolly() and scrollTo() to scroll to pixel positions, but due to the change in View widths, lines become longer and a line that was at pixel pos 400 might now be at 250. So this is not very helpful.
I need a way to find the first visible line in a TextView in onDestroy() and then a way to make the TextView scroll to this specific piece of text after the rotation.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一个老问题,但我在寻找同一问题的解决方案时来到这里,所以这就是我想到的。我结合了这三个问题的答案的想法:
我尝试从我的应用程序中仅提取相关代码,因此请原谅任何错误。另请注意,如果您旋转到横向并返回,它可能不会以您开始的位置结束。例如,假设“Peter”是肖像中第一个可见的单词。当您旋转到横向时,“Peter”是该行的最后一个单词,第一个单词是“Larry”。当您向后旋转时,“Larry”将可见。
This is an old question, but I landed here when searching for a solution to the same problem, so here is what I came up with. I combined ideas from answers to these three questions:
I tried to extract only the relevant code from my app, so please forgive any errors. Also note that if you rotate to landscape and back, it may not end in the same position you started. For example, say "Peter" is the first visible word in portrait. When you rotate to landscape, "Peter" is the last word on its line, and the first is "Larry". When you rotate back, "Larry" will be visible.
TextView 可以为您保存和恢复其状态。如果您无法使用它,您可以禁用它并显式调用方法:
http://developer.android.com/reference/android/widget/TextView.SavedState.html
http://developer.android.com/reference/android/widget/TextView.html#onSaveInstanceState()
http://developer.android.com/reference /android/widget/TextView.html#onRestoreInstanceState(android.os.Parcelable)
TextView can save and restore its state for you. If you aren't able to use that, you can disable that and explicitly call the methods:
http://developer.android.com/reference/android/widget/TextView.SavedState.html
http://developer.android.com/reference/android/widget/TextView.html#onSaveInstanceState()
http://developer.android.com/reference/android/widget/TextView.html#onRestoreInstanceState(android.os.Parcelable)
最佳答案,我通过搜索得到的。
}
The best answer, I got by searching.
}