如何在android中找到视图位置
如何找到android中的视图位置?
为了获取位置,我使用了 view.getLeft()、view.getRight()、view.getTop() 和 view。 getBottm()。但这些方法返回0
(零)。我在调用 setContentView() 方法后使用了这些方法。
如果有人知道获取视图位置的代码,请帮助我。
How can I find the view position in android?
To get the position I used view.getLeft()
, view.getRight()
, view.getTop()
and view.getBottm()
. But these methods are returning 0
(Zero). I used these methods after calling the setContentView()
method.
If anyone know the code to get the view position, please help me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这在 2.2 中对我有用:
您正在寻找的代码是:
这应该返回视图“矩形”中的以下位置
,即视图顶部的 Y(垂直)坐标。
请注意,这是基于您所使用的布局类型的上下文。这是在 ScrollView 中进行测试的,如上所示。
希望这有帮助!
This worked for me in 2.2:
The bit of the code you're looking for is:
This should return the following spot in a view's "rectangle"
I.e., the Y (vertical) coordinate of the top of the view.
Note that this is contextual based on what kind of Layout you're using. This was tested in a ScrollView, as seen above.
Hope this helps!
您在哪里调用
getLeft()
、getTop()
等?在调用onMeasure()
之前不会设置视图的位置,因此这些方法在此之前将返回 0。onCreate()
在onMeasure()
之前调用,因此调用getLeft()
在onCreate() 中没有多大用处)
。您可能需要将代码移至onMeasure()
或onResume()
。但是,如果您确实重写了onMeasure()
,请注意它通常会被调用多次。Where were you calling
getLeft()
,getTop()
, etc? A view's position isn't set untilonMeasure()
is called, and so those methods will return 0 until then.onCreate()
is called sometime beforeonMeasure()
, and so callinggetLeft()
won't be much use inonCreate()
. You may want to move your code toonMeasure()
oronResume()
. If you do overrideonMeasure()
, however, be aware that it is usually called several times.