如何获取ScrollView中视图的偏移量? [英] How to get the offset of a view inside a ScrollView?

查看:598
本文介绍了如何获取ScrollView中视图的偏移量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个ScrollView,其中有许多子视图.有什么办法可以将每个孩子的offset-y移到ScrollView的顶部吗?

There is a ScrollView and which has many children views. Is there any way to get the offset-y to the top of the ScrollView for every child?

推荐答案

嗯,不确定这是否是最快,最好的方法,但是如果您在屏幕上看到了视图的位置,并且知道ScrollView,您可以计算视图相对于ScrollView的偏移量.要获取视图在屏幕中的位置,请执行以下操作:

Hmmmm, not sure if this is the quickest, best way to do this, but if you get the view's location on screen, and you know the top of the ScrollView, you can calculate the offset of the view relative to the ScrollView. To get the position of a View in the screen:

int[] location = {0,0};
view.getLocationOnScreen(location);

location[1]具有Y值.当视图位于视口上方"时,您将获得负值.

location[1] has the Y value. You will get negative values when the view is "above" the viewport.

这篇关于如何获取ScrollView中视图的偏移量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆