可以使用 smoothScrollTo(int x, int y) 方法将 ScrollView 滑动到指定的 View

例如,假设你有一个 ScrollView 和一个 TextView,你想使 ScrollView 滑动到 TextView

首先,你需要获取 TextView 的坐标,然后使用 smoothScrollTo() 方法将 ScrollView 滑动到 TextView 的位置。

示例代码如下:

ScrollView scrollView = findViewById(R.id.scroll_view);
TextView textView = findViewById(R.id.text_view);

// 获取 TextView 的坐标
int[] location = new int[2];
textView.getLocationOnScreen(location);

// 滑动 ScrollView 到 TextView 的位置
scrollView.smoothScrollTo(0, location[1]);

在上面的代码中,我们首先获取 TextView 的坐标,然后使用 smoothScrollTo() 方法将 ScrollView 滑动到 TextView 的位置。

注意,我们使用 location[1] 作为纵向滑动的距离,因为在 ScrollView 中,纵向滑动是使用 y 坐标来确定的。如果你想滑动到 TextView 的底部,可以将 location[1] 的值增加 textView.getHeight()

另外,你还可以使用 smoothScrollBy(int dx, int dy) 方法在当前位置滑动指定的距离,而不是滑动到指定的位置。

Android ScrollView 滑动到指定 View: 使用 smoothScrollTo() 方法

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

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