可能的原因:

  1. scrollTo()方法的参数是以像素为单位的,需要确保在ScrollView的可滚动范围内。

  2. ScrollView必须在layout完成后才能使用scrollTo()方法。可以在onWindowFocusChanged()方法中调用scrollTo()方法。

  3. ScrollView的子View必须填满整个ScrollView才能使用scrollTo()方法。可以在ScrollView中添加一个LinearLayout,并将子View添加到该LinearLayout中。

  4. ScrollView可能被其他View遮挡。可以在ScrollView上方添加一个透明的View,使其成为顶层View。

示例代码:

ScrollView scrollView = findViewById(R.id.scroll_view);
scrollView.post(new Runnable() {
    @Override
    public void run() {
        scrollView.scrollTo(0, 500); // 滚动到y轴500像素处
    }
});

在布局文件中,需要将ScrollView的子View放入一个LinearLayout中,并设置android:fillViewport="true"属性:

<ScrollView
    android:id="@+id/scroll_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="16dp"
        android:gravity="center_horizontal"
        android:background="#ffffff"
        android:fillViewport="true">

        <!-- 子View -->

    </LinearLayout>

</ScrollView>
``
android scrollView scrollTo无效

原文地址: https://www.cveoy.top/t/topic/g2p9 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录