Android在更改文本大小时保持滚动相对位置
我正在开发一个包含线性布局的项目,该布局由许多文本视图组成,所有文本视图都由滚动视图包裹。 用户可以选择在对话框中更改文本大小。我希望当文本大小发生变化时保留用户的相对滚动位置。
例如:假设我们有 10 个文本视图,用户当前滚动到第三个文本视图的中间。我希望用户在更改文本大小后仍或多或少地滚动到第三个的中间。
我尝试这样做:
double scrollViewHeight = sv.getChildAt(0).getBottom() - sv.getHeight();
double getScrollY = sv.getScrollY();
double scrollPosition = (getScrollY / scrollViewHeight) * 100d;
// the relevant part of this chunk is the changing of the text size
int textSize = ++((SettingsDialogFragment) dialog).currentTextSize;
if(textSize > 100) --((SettingsDialogFragment) dialog).currentTextSize;
for(int i=0; i<ll.getChildCount();++i){
TextView tv = (TextView) ll.getChildAt(i);
tv.setTextSize(textSize);
}
double newSVHeigth = sv.getChildAt(0).getBottom() - sv.getHeight();
sv.smoothScrollTo(0, (int) ((int) scrollPosition*newSVHeigth));
所以基本上是滚动比例并在新高度内滚动该比例。 问题是 newSVHeigth 获得与 scrollViewHeight 相同的值,并且它们只会在下次调用该函数时更改(按钮单击的侦听器)。 因此,滚动视图仅在该代码块完成后对更改做出反应。 下一次,sv.getChildAt(0).getBottom()会更新,但sv.getHeight()会更新。我不确定为什么会这样,但是 sv.getChildAt(0).getBottom() - sv.getHeight() 似乎给出了正确的结果。
有没有更简单的方法来做到这一点? 如果没有,新的 scrollViewHeight 将在哪里更新以及如何收听更改?
谢谢
I'm working on a project containing a linear layout consisting of many text views, all wrapped by a scroll view.
The User has the option of changing text size within a dialog. I would like the user's relative scroll position to be kept when the text size changes.
For example: Say we have 10 text views and the user is currently scrolled to the middle of the third one. I would like the user to still be scrolled more or less to the middle of the third one after changing the text size.
I've tried doing this:
double scrollViewHeight = sv.getChildAt(0).getBottom() - sv.getHeight();
double getScrollY = sv.getScrollY();
double scrollPosition = (getScrollY / scrollViewHeight) * 100d;
// the relevant part of this chunk is the changing of the text size
int textSize = ++((SettingsDialogFragment) dialog).currentTextSize;
if(textSize > 100) --((SettingsDialogFragment) dialog).currentTextSize;
for(int i=0; i<ll.getChildCount();++i){
TextView tv = (TextView) ll.getChildAt(i);
tv.setTextSize(textSize);
}
double newSVHeigth = sv.getChildAt(0).getBottom() - sv.getHeight();
sv.smoothScrollTo(0, (int) ((int) scrollPosition*newSVHeigth));
So basically getting the proportion scrolled and scrolling that proportion within the new height.
The problem is newSVHeigth gets the same value as scrollViewHeight and they would only change the next time the function is called (a listener to a button click).
So the scroll view only reacts to the changes after this block of code is finished.
On that next time, sv.getChildAt(0).getBottom() is getting updated but sv.getHeight(). I'm not sure why that is but sv.getChildAt(0).getBottom() - sv.getHeight() is seeming to give the correct result anyway..
Is there an easier way to do this?
If not, where would the new scrollViewHeight be updated and how can I listen to the change?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我最终找到了这个解决方案:
我在文本视图列表的最后一项上定义了 LayoutChangeListener。
这样,每当我更改文本大小和布局更改时,滚动都会更新。
I eventually found this solution:
I defined a LayoutChangeListener on the last item of the list of text views.
This way whenever I change the text size and the layout changes the scroll is updated.