机器人.getTop()返回0.0 [英] android .getTop() returns 0.0

查看:762
本文介绍了机器人.getTop()返回0.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Android锁屏应用。我的想法得到了从另一个线程来检查哪里手指有pssed下来攻击到我已成立了ImageButton的坐标$ P $的坐标。

I am developing an android lock screen app. I got the idea from another thread to check the coordinates of where the finger has pressed down against to the coordinates of the imagebutton that I have set up.

我试图用得到的ImageButton的坐标 .getTop() .getLeft()但两者都返回0.0(因为它们是花车,不是int)。

I am trying to get the coordinates of the imagebutton using the .getTop() and .getLeft() but both are returning 0.0 (as they are floats, not int).

这是为什么?我是新android开发,很抱歉,如果我失去了一些东西明显。

why is this? I am new to android development, so sorry if I am missing something obvious.

另外,我读的地方,在 .getTop()的setContentView(R.layout.layoutname)后,将无法正常工作,但如果我把它上面的setContentView(...)然后应用程序崩溃。

Also, I read somewhere that the .getTop() won't work after the setContentView(R.layout.layoutname) but if I put it above setContentView(...) then the app crashes.

有另一种方式来获得的ImageButton的坐标,或者会更好/更容易得到表格单元格的坐标和边界?我会怎么做呢?

Is there another way to get the coordinates of the imagebutton, or would it be better/easier to get the coordinates and bounds of the table cell ? How would I do that?

在此先感谢:)

推荐答案

您需要您的视图添加到布局的并显示(至少有一个布局传递和计量合格必须有运行)。另外,如此获得的坐标是相对于视图的直接父

You need your view added to a layout and displayed (At least one layout pass and measure pass must have run.). Also, the co-ordinates so obtained are relative to view's immediate parent.

有可能通过调用以检索一个视图的位置
  方法getLeft()和共达()。前者返回的左侧,或X,
  矩形重新presenting视图的坐标。后者返回
  矩形重新presenting视图的顶部,或Y,坐标。
  这些方法都返回视图的相对的位置​​,其
  父
。例如,当getLeft()返回20,这意味着图
  位于20个像素的直接的左边缘的右边
  家长。

It is possible to retrieve the location of a view by invoking the methods getLeft() and getTop(). The former returns the left, or X, coordinate of the rectangle representing the view. The latter returns the top, or Y, coordinate of the rectangle representing the view. These methods both return the location of the view relative to its parent. 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.

这篇关于机器人.getTop()返回0.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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