//默认执行,计算view的宽高,在onDraw()之前
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = measureWidth(widthMeasureSpec);
int height = measureHeight(heightMeasureSpec);
//设置宽高
setMeasuredDimension(width, height);
}
//根据xml的设定获取宽度
private int measureWidth(int measureSpec) {
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
//wrap_content
if (specMode == MeasureSpec.AT_MOST){
}
//fill_parent或者精确值
else if (specMode == MeasureSpec.EXACTLY){
}
Log.i("这个控件的宽度----------","specMode=" specMode " specSize=" specSize);
return specSize;
}
//根据xml的设定获取高度
private int measureHeight(int measureSpec) {
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
//wrap_content
if (specMode == MeasureSpec.AT_MOST){
}
//fill_parent或者精确值
else if (specMode == MeasureSpec.EXACTLY){
}
Log.i("这个控件的高度----------","specMode:" specMode " specSize:" specSize);
return specSize;
}