开发者

Android: Problem with onMeasure()

开发者 https://www.devze.com 2022-12-30 17:48 出处:网络
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?

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.
0

精彩评论

暂无评论...
验证码 换一张
取 消