View的getTop()、getBottom()、getLeft()、getRight()获取的都是当前View相对于它的父类容器的顶部、底部、左边和右边的位置是相对位置
看下图:
right = left + width;
bottom = top + height;
再来看一看getHeight(),getWidth(),他们是这样的
总结(正解):
getWidth()/getHeight: View在设定好布局后整个View的宽度/高度。
getMeasuredWidth(): 对View上的内容进行测量后得到的View内容佔据的宽度,前提是你必须在父布局的onLayout()方法或者此View的onDraw()方法裡调 用measure(0,0);(measure 参数的值你可以自己定义),否则你得到的结果和getWidth()得到的结果一样。
那如何获取View的绝对位置呢?
http://www.cnblogs.com/mengdd/p/3273284.html
getGlobalVisibleRect(Rect r)是可以得到绝对坐标的。
getLocationInWindow(int[] location) 返回View左上角的绝对坐标
getLocationOnScreen(int[] location) 返回View左上角的绝对坐标
这两个方法目前还不出有什么差别。
直接看代码
private String getViewInfo(View view) {
StringBuffer stringBuffer = new StringBuffer();
int top = view.getTop();
int left = view.getLeft();
int right = view.getRight();
int bottom = view.getBottom();
int width = view.getWidth();
int height = view.getHeight();
stringBuffer.append("Info relative to Parent: " + "left: " + left
+ ", right: " + right + ", top: " + top + ", bottom: " + bottom
+ ", width: " + width + ", height: " + height);
// API Level 11
stringBuffer.append("\n view.getTranslationX(): "
+ view.getTranslationX());
stringBuffer.append("\n view.getTranslationY(): "
+ view.getTranslationY());
Rect rect = new Rect();
view.getLocalVisibleRect(rect);
stringBuffer.append("\ngetLocalVisibleRect(): ");
stringBuffer.append("\n " + rect.toString());
Rect globalRect = new Rect();
view.getGlobalVisibleRect(globalRect);
stringBuffer.append("\ngetGlobalVisibleRect(): ");
stringBuffer.append("\n " + globalRect.toString());
int[] locationInWindow = new int[2];
view.getLocationInWindow(locationInWindow);
stringBuffer.append("\ngetLocationInWindow(): ");
stringBuffer.append("\n " + locationInWindow[0] + ", "
+ locationInWindow[1]);
int[] locationOnScreen = new int[2];
view.getLocationOnScreen(locationOnScreen);
stringBuffer.append("\ngetLocationOnScreen(): ");
stringBuffer.append("\n " + locationOnScreen[0] + ", "
+ locationOnScreen[1]);
return stringBuffer.toString();
}