获取控件坐标位置一直是0

929 阅读1分钟

    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方法之后被调用,可以得到真正的位置