int[] viewLocationArray = new int[2];
textview.getLocationOnScreen(viewLocationArray);
复制代码
在onCreate的方法里,调用该方法得到都是0,是因为UI还在刷新,位置还没有被计算出来
textview.post(new Runnable(){
@Override
public void run(){
int left=imageview.getLeft();//得到了正确的值
int right=imageview.getRight();
int top=imageview.getTop();
int bottom=imageview.getBottom();
}
};
复制代码
post方法会在window的attachedToWindow方法之后被调用,可以得到真正的位置