measuring a view before rendering it

后端 未结 5 1149
傲寒
傲寒 2020-11-29 01:37

I need to find out how big a view will be after attaching it to its parent.

I have overridden this method:

onMeasure(int, int);

but

5条回答
  •  伪装坚强ぢ
    2020-11-29 01:47

    OnMeasure does not tell you the size of the View. Instead, it asks your custom View to set its size by providing some constraints enforced by the parent View.

    This is from the SDK documentation:

    The first pair is known as measured width and measured height. These dimensions define how big a view wants to be within its parent (see Layout for more details.) The measured dimensions can be obtained by calling getMeasuredWidth() and getMeasuredHeight().

    The second pair is simply known as width and height, or sometimes drawing width and drawing height. These dimensions define the actual size of the view on screen, at drawing time and after layout. These values may, but do not have to, be different from the measured width and height. The width and height can be obtained by calling getWidth() and getHeight().

    After layout you can call getWidth() and getHeight() to find the final size of your custom View.

提交回复
热议问题