【问题标题】:get position of view with respect to top-most parent in android获取相对于android中最顶层父级的视图位置
【发布时间】:2014-07-30 19:46:52
【问题描述】:

要获得相对于其父级的视图,我可以使用getLeft()getRight()getTop()getBottom()。但是如何获得相对于布局文件最顶层父级的视图? 所以说我的前夕布局是RelativeLayout。然后说在前夕我有许多嵌套布局。然后在层次结构的深处,我有一个 EditText 视图。如何获取 EditText 视图相对于 eve 的位置?

【问题讨论】:

  • 你想用这些信息做什么?相对于根的位置对于将视图定位在嵌套子级中没有多大帮助,因为视图位置始终与其直接父级相关。

标签: android android-layout


【解决方案1】:

要查找绝对视图位置,您可以使用view.getLocationOnScreen()view.getLocationInWindow()

【讨论】:

    【解决方案2】:
    View target = findViewById(R.id.target_id);
    View parent = (View)target.getParent();
    int x = target.getLeft();
    int y = target.getTop();
    while(parent != null){
       x += parent.getLeft();
       y += parent.getTop();
       parent = (View)parent.getParent()
    }
    

    这些代码可能会对你有所帮助,因为我没有测试它。我希望它会起作用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-15
      相关资源
      最近更新 更多