Android View测量模式

今天看android源码的时候无意中发现了源码中关于测量模式的解释,简单的记录一下,一看就是恍然大悟了。

  int result = desiredSize;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize =  MeasureSpec.getSize(measureSpec);
switch (specMode) {
    case MeasureSpec.UNSPECIFIED:
        /* Parent says we can be as big as we want. Just don't be larger
           than max size imposed on ourselves.
        */
        result = Math.min(desiredSize, maxSize);
        break;
    case MeasureSpec.AT_MOST:
        // Parent says we can be as big as we want, up to specSize. 
        // Don't be larger than specSize, and don't be larger than 
        // the max size imposed on ourselves.
        result = Math.min(Math.min(desiredSize, specSize), maxSize);
        break;
    case MeasureSpec.EXACTLY:
        // No choice. Do what we are told.
        result = specSize;
        break;
}
return result;

看着代码中的注释应该很容易明白以后测量模式应该怎么用了。