6

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

getScrollX()

Return the scrolled left position of this view.

getLeft()

Left position of this view relative to its parent

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

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

显然它们不能相同,否则没有必要有两种不同的方法返回相同的结果

请帮忙

4

1 回答 1

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. 

getScrollX(),另一方面,让您知道视图中的内容是如何移动的。

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

于 2013-04-05T04:08:04.100 回答