View.getLeft()与View.getScrollX()之间的Android差异

Xyz*_*ter 6 android android-widget android-view

View.getLeft()与View.getScrollX()有什么区别?请不要复制和粘贴文档中的定义,因为我将在下面为您执行此操作

getScrollX()

Return the scrolled left position of this view.
Run Code Online (Sandbox Code Playgroud)

getLeft()

Left position of this view relative to its parent
Run Code Online (Sandbox Code Playgroud)

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

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

显然它们不能相同,否则就不需要有两种不同的方法可以返回相同的结果

请帮忙

Hal*_*alR 3

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

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. 
Run Code Online (Sandbox Code Playgroud)

getScrollX()另一方面,可以让您了解视图中的内容如何移动。

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