在一个活动中获取片段的绝对位置
问题描述:
我有一个活动,里面有一些片段。我为每个片段使用了“框架布局”,并将“相对布局”作为父项。在一个活动中获取片段的绝对位置
我一直在头痛得到父母片段的确切位置。但似乎我只能够相对价值。
那么,无论如何要得到活动中片段的确切位置吗?
答
我还没有用碎片做过这件事,但碎片的内容。 如果您在活动中使用findById()
,该活动将查找该活动中充气的任何内容的标识(包括片段内容等)。
然后,您可以使用类似View.getLocationOnScreen(int[])的东西,这应该给你屏幕上的视图的x和y。从来没有尝试过这个片段,但如果你的片段被布局包裹,那么应该没问题。
希望这会帮助吗?
其实这可能帮助,可能需要改变采取两种意见,这种方法是一个自定义的RelativeLayout我做内:
/**
* Will return the X,Y of the Anchor view relative to this relative layout.. So It doesn't
* matter if the anchor view is nested, or any any other layout, this WILL find its location
* relative too this {@link HelpRelativeLayout}.
*
* @author chris.jenkins
* @param anchor
* @return
*/
private int[] calculateAnchorViewPosition(View anchor)
{
int[] loc = new int[2];
int[] conLoc = new int[2];
// getLocationInWindow(conLoc);
getLocationOnScreen(conLoc);
// anchor.getLocationInWindow(loc);
anchor.getLocationOnScreen(loc);
// get correct top left pos + half the h/w of the anchor
loc[0] = (loc[0] - conLoc[0]);
loc[1] = (loc[1] - conLoc[1]);
MyLog.d(loc[0] + " " + loc[1]);
return loc;
}
为什么你会需要这些信息?绝对定位是要避免在Android上。 – CommonsWare 2011-06-01 12:15:28
我想用它与动画师 – 2011-06-04 07:07:15