如何在AbsoluteLayout中获取视图的位置?

时间:2010-01-08 13:11:10

标签: android

任何人都可以给我一个如何获取视图位置的提示,这是AbsoluteLayout的孩子吗?我想这样做是为了拖放选定的视图。

谢谢!

2 个答案:

答案 0 :(得分:2)

不推荐使用AbsoluteLayout,因此您可能需要通过扩展ViewGroup来提供自己的拖放布局。一般来说,布局它负责定位子窗口小部件。这是在onLayout()方法中完成的,您必须覆盖它。它可能是这样的:


protected void onLayout(boolean changed, int l, int t, int r, int b) {
   final int count = getChildCount();
   for(int i=0; i<count; i++){
      final View child = getChildAt(i);
      if(GONE != child.getVisibility()){
        //position child
        child.layout(left, top, right, bottom);
      }
   }
}

因此,通过实现自己的DragAndDropLayout - 您知道您孩子的位置。 但是,也许有更简单的解决方案 此致!

答案 1 :(得分:2)

要知道孩子在其父母的位置,只需调用getLeft()和getTop()即可。另外,不要使用AbsoluteLayout:)