2012-07-12 49 views

回答

1

我觉得这样的事情应该工作:

  1. 走你的视图结构,找到孩子的意见(即那些没有子视图)是可见的。

  2. 使用View.getLocationOnScreen()在你的视图来检索他们的位置(上/左窗口坐标)

  3. 使用getMeasuredWidth()/ getMeasuredHeight()来获取视图的宽度和高度

  4. 看看你的这个像素矩形内的坐标落在

3

如果你是ViewGroup内:

int count = viewgroup.getChildCount(); 
for (int i = 0; i < count; i++) 
{ 
    View view = viewgroup.getChildAt(i); 
    if (view.getX() == theX && view.getY() == theY) 
      return view.getId() 
} 

EDIT(kcoppock):中的for循环,我会做这样的事情:

View view = viewgroup.getChildAt(i); 
if(!view.getVisibility() == View.VISIBLE) continue; 
int[] location = {0, 0}; 
view.getLocationOnScreen(location); 
int right = location[0] + view.getWidth(); 
int bottom = location[1] + view.getHeight(); 
Rect bounds = new Rect(location[0], location[1], right, bottom); 
if(bounds.contains(coordinatesX, coordinatesY)) return view.getId(); 
+0

这已经很接近,但只打算当他点击左上角的确切像素的工作的观点。我会在一个建议中编辑。 – kcoppock 2012-07-12 15:03:20

+0

确切地感谢您编辑 – Blackbelt 2012-07-12 15:04:59

+1

。您还需要确保getChildAt()返回可见的视图(getVisibility()== View.VISIBLE) – CSmith 2012-07-12 15:14:38