View.getLeft() 与 View.getScrollX() 之间的区别

标签 android android-widget android-view

有什么区别 View.getLeft() 与 View.getScrollX() 相比? 请不要从文档中复制和粘贴定义,因为我将在下面为您做这件事

getScrollX()

Return the scrolled left position of this view.

获取左边()

Left position of this view relative to its parent

我认为这 2 个值应该相同,但是我的示例程序,如果我执行 View.scrollBy(20, 0) 我看到 getScrollX() 将返回 20 并且 View 实际上向右移动,但 getLeft() 仍然为零

我很困惑,因为如果视觉上 View 向右滚动 20px,它的左侧位置也应该更新,但它仍然是 0

显然它们不能相同,否则不需要有2个不同的方法返回相同的结果

请帮忙

最佳答案

getLeft() 返回相对于其父 View 的 View 位置。它的滚动方式根本不会影响这一点。滚动影响 View 的内容,而不是它的位置。

Android 文档中有关 getLeft() 的引述:

For instance, when getLeft() returns 20, that means the view is located 20 pixels to 
the right of the left edge of its direct parent. 
另一方面,

getScrollX() 让您知道 View 中的内容是如何移动的。

View.scrollBy(20,0) 影响 View 中的内容(如 View 的 subview )并且实际上并不相对于 View 的父 View 移动 View 。

关于View.getLeft() 与 View.getScrollX() 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15825024/

相关文章:

java - 如何将 Theta 360 度照片发布到 Facebook

android - getContentResolver().query android where 子句

php - 无法在服务器中插入信息,但我可以作为本地主机

Android EditText软键盘问题

android - 在不调用 Dismiss 的情况下关闭警报对话框

android - 如何以编程方式在android中随机生成id?

android - ViewGroup_instance.addView(View_instance),子类如何接受父类(super class)实例?

java - 添加到数组时强制关闭

android - 如何为下面的 ArrayList 制作下一个和上一个按钮代码?

android - 改变 RadioButton 的背景覆盖单选按钮 android 上的文本