onMeasure()--典型视图度量时间

@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		measuredHeight = measureHeight(heightMeasureSpec);
		measuredWidth = measureWidth(widthMeasureSpec);
		setMeasuredDimension(measuredWidth, measuredHeight);
	}

	private int measureWidth(int widthMeasureSpec) {
		int specMode = MeasureSpec.getMode(widthMeasureSpec);
		int specSize = MeasureSpec.getSize(widthMeasureSpec);
		
		//如果不指定限制,就是默认大小
		int result = 50;
		if(specMode == MeasureSpec.AT_MOST){
			//如果父组件被控件填充,则返回外边界大小
			result = specSize;
		}else if(specMode == MeasureSpec.EXACTLY){
			//如果指定空间大小,返回控件实际大小
			result = specSize;
		}
		return result;
	}

	private int measureHeight(int heightMeasureSpec) {
		// TODO Auto-generated method stub
		int specMode = MeasureSpec.getMode(heightMeasureSpec);
		int specSize = MeasureSpec.getSize(heightMeasureSpec);
		
		//如果不指定限制,就是默认大小
		int result = 50;
		if(specMode == MeasureSpec.AT_MOST){
			//如果父组件被控件填充,则返回外边界大小
			result = specSize;
		}else if(specMode == MeasureSpec.EXACTLY){
			//如果指定空间大小,返回控件实际大小
			result = specSize;
		}
		return result;
	}
	
	

猜你喜欢

转载自kingbinchow.iteye.com/blog/1807381