android - 如何通过坐标 x,y Android 在 View 中查找元素

标签 android

如果我知道坐标(X,Y) 像素(通过OnTouchEvent 方法和getX(),getY)我如何找到元素ex。按钮或文本等....使用X,Y

最佳答案

您可以对每个 subview 使用 getHitRect(outRect) 并检查该点是否在生成的矩形中。这是一个快速示例。

for(int _numChildren = getChildCount(); --_numChildren)
{
    View _child = getChildAt(_numChildren);
    Rect _bounds = new Rect();
    _child.getHitRect(_bounds);
    if (_bounds.contains(x, y)
        // In View = true!!!
}

希望对您有所帮助,

模糊逻辑

关于android - 如何通过坐标 x,y Android 在 View 中查找元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10959400/

相关文章:

java - 如何为 webview 的多个实例创建类或方法

java - 链接谷歌播放服务后,Eclipse 崩溃并超出 GC 开销限制

android - fragment 被方向改变的第一个 fragment 取代

java - 在 ListView 中删除标题的背景

android - 从 Activity B 导航到 Activity A 时的明确 Intent

java - 拍摄照片到外部存储<智能手机中的图库>

java - Android MediaPlayereekTo()/getDuration() 未按预期工作

Google Play 商店中的 Android 版本 : Where is the Opt-in URL?

android - 如何绕过 TextView 元素的文本限制?

java - 当用户捕获图像时,如何检查 android 中当前打开的相机 ID(主要或次要)?