如何以编程方式滚动的Horizo​​ntalScrollView方式、Horizo、ntalScrollView

2023-09-04 06:21:32 作者:孤心大叔.

我有一个 Horizo​​ntalScrollView ,其中包含一个 RelativeLayout的。这种布局是空的XML,并从Java在OnCreate填充。

I have an HorizontalScrollView which contains a RelativeLayout. This layout is empty in the XML, and is populated from java in the onCreate.

我想这个滚动视图是在中间的某个地方开始的 RelativeLayout的,这是远远大于屏幕。

I would like this scroll view to be initially somewhere in the middle of the RelativeLayout, which is way larger than the screen.

我试过 mHorizScrollView.scrollTo(offsetX,0)不工作。 我不知道什么是错。

I tried mHorizScrollView.scrollTo(offsetX, 0); which doesn't work. I don't know what's wrong with this.

我可以张贴了code,但它不是真正相关。要紧的是,一切程序来完成(有:秒),而初始位置 Horizo​​ntalScrollView 已被编程设置

I could post the code, but it is not really relevant. What matters is that everything is done programatically (has to :s), and that the initial position of the HorizontalScrollView has to be set programmatically.

感谢您的阅读。请告诉我,如果你需要更多的细节,或者如果这还不够清楚。

Thanks for reading. Please tell me if you need more details or if this is not clear enough.

推荐答案

我发现,如果你延长 Horizo​​ntalScrollView 并覆盖 onLayout ,你可以清晰地滚动,超级调用后 onLayout

I found that if you extend the HorizontalScrollView and override the onLayout, you can cleanly scroll, after the super call to onLayout:

MyScrollView extends HorizontalScrollView {
    protected void onLayout (boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        this.scrollTo(wherever, 0);
    }
}