Android: Problem with onMeasure()
I made a custom view. If I add the view to the layout XML file and I set the height to fill_parent
"specSize" return 0. Why?
Code:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int measuredHeight = 90;
int specMode = MeasureSpec.getMode(heightMeasureSpec);
int specSize = MeasureSpec.getSize(MeasureSpec.getMode(heightMeasureSpec));
if(specMode != Meas开发者_StackOverflowureSpec.UNSPECIFIED){
measuredHeight = specSize;
}
setMeasuredDimension(60, measuredHeight);
}
Does anyone know how I can get the height of fill_parent
?
You don't need to call MeasureSpec.getMode
inside of call to 'getSize' whole idea of measure spec is to combine way of measuring ( knowing as Spec) and associated Size. See documentation for MeasureSpec.makeMeasureSpec
method.So correct code will look something like:
int widthSpec = MeasureSpec.getMode(widthMeasureSpec);
int width = MeasureSpec.getSize(widthMeasureSpec);
The correct code will be:
int specMode = MeasureSpec.getMode(heightMeasureSpec); ----This was already correct
int specSize = MeasureSpec.getSize(heightMeasureSpec); ----This is corrected.
精彩评论