View中的属性也是挺多的,搞得初学者都有点懵逼,今天就来捋一捋。
1、mLeft、mTop、mRight、mBottom定义了控件的区域(左上角和右下角的坐标),都有对应的get、set方法,其中mLeft表示控件左边距离父控件左边的距离,mRight表示控件右边距离父控件左边的距离,mTop和mBottom类似;
2、getWidth和getHeight,源码如下:
public final int getWidth() { return mRight - mLeft; }
public final int getHeight() { return mBottom - mTop; }由上面的源代码可以发现宽高其实是由mRight-mLeft以及mBottom-mTop计算出来的,看到这里估计大家就知道为什么 这两个方法在layout方法之后就可以正常获取到控件的宽高了吧,原因很简单: 因为layout之后mRight,mLeft,mTop,mBottom就确定了。
3、getMeasureWidth和getmeasureHeight,获取控件的测量值,在onMeasure方法之后就可以获取正常值,其中onMeasure方法中的核心方法是setOnMeasureDimension。
4、getX、setX、getY和setY方法,源码如下:
/** * The visual x position of this view, in pixels. This is equivalent to the * { @link #setTranslationX(float) translationX} property plus the current * { @link #getLeft() left} property. * * @return The visual x position of this view, in pixels. */
public float getX() { return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0); }
/** * Sets the visual x position of this view, in pixels. This is equivalent to setting the * { @link #setTranslationX(float) translationX} property to be the difference between * the x value passed in and the current { @link #getLeft() left} property. * * @param x The visual x position of this view, in pixel