Android在更改文本大小时保持滚动相对位置

发布于 2025-01-12 10:12:45 字数 1387 浏览 0 评论 0原文

我正在开发一个包含线性布局的项目,该布局由许多文本视图组成,所有文本视图都由滚动视图包裹。 用户可以选择在对话框中更改文本大小。我希望当文本大小发生变化时保留用户的相对滚动位置。

例如:假设我们有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初见你 2025-01-19 10:12:45

我最终找到了这个解决方案:
我在文本视图列表的最后一项上定义了 LayoutChangeListener。

text_views_list.get(text_views_list.size()-1)
.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
                @Override
                public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                double scrollViewHeight = oldBottom - sv.getHeight();
                double getScrollY = sv.getScrollY();
                double scrollPosition = (getScrollY / scrollViewHeight);
                double newSVHeigth = bottom - sv.getHeight();
                sv.smoothScrollTo(0, (int) (scrollPosition*newSVHeigth));
                 }
 });

这样,每当我更改文本大小和布局更改时,滚动都会更新。

I eventually found this solution:
I defined a LayoutChangeListener on the last item of the list of text views.

text_views_list.get(text_views_list.size()-1)
.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
                @Override
                public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                double scrollViewHeight = oldBottom - sv.getHeight();
                double getScrollY = sv.getScrollY();
                double scrollPosition = (getScrollY / scrollViewHeight);
                double newSVHeigth = bottom - sv.getHeight();
                sv.smoothScrollTo(0, (int) (scrollPosition*newSVHeigth));
                 }
 });

This way whenever I change the text size and the layout changes the scroll is updated.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文