Android: problem with onMeasure ()

I made a custom look. If I add a view to the XML layout file and I set the height to fill_parent"specSize" return 0. Why?

the 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 != MeasureSpec.UNSPECIFIED){
         measuredHeight = specSize;
        }
        setMeasuredDimension(60, measuredHeight);
}

Does anyone know how I can get the height fill_parent?

+3
source share
2 answers

You do not need to MeasureSpec.getModecall getSize inside the call. The whole idea of ​​measuring spec is to combine the way of measuring (knowledge as Spec) and the size associated with it. See the documentation for the method MeasureSpec.makeMeasureSpec. The correct code would look something like this:

int widthSpec = MeasureSpec.getMode(widthMeasureSpec);
int width = MeasureSpec.getSize(widthMeasureSpec);
+3
source

The correct code would be:

int specMode = MeasureSpec.getMode(heightMeasureSpec);     ----This was already correct
int specSize = MeasureSpec.getSize(heightMeasureSpec);     ----This is corrected.
0
source

Source: https://habr.com/ru/post/1745274/


All Articles