问题描述
|
我的活动中有几个片段。我正在为每个片段使用“帧布局”,并且将“相对布局”作为父级。
我一直很想获得片段在父级中的确切位置。但是好像我只能相对值。
因此,是否总有一个活动中某个片段的确切位置?
解决方法
我还没有使用片段执行此操作,而是使用了片段的内容。
如果在活动中使用“ 0”,则会发现该活动中所有虚高的内容的ID(包括片段内容等)。
然后,您可以使用诸如View.getLocationOnScreen(int [])之类的东西,这应该为您提供屏幕上视图的x和y。从未尝试过对片段进行尝试,但是如果您的片段被Layout包裹,那么应该没有问题。
希望这可以帮助?
实际上,这可能会有所帮助,可能需要更改为采用两个视图,因为此方法位于我制作的自定义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;
}