2012-07-12 53 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