What is the difference between getWidth/Height() and getMeasuredWidth/Height() in Android SDK?

前端 未结 1 501
终归单人心
终归单人心 2020-12-02 07:50

The Android Documentation says that there is two sizes for a view, the measured dimensions and the drawing dimensions. The measured dimension is the one co

相关标签:
1条回答
  • 2020-12-02 08:31

    As the name suggests the measuredWidth/height is used during measuring and layoutting phase.

    Let me give an example,

    A widget is asked to measure itself, The widget says that it wants to be 200px by 200px. This is measuredWidth/height.

    During the layout phase, i.e. in onLayout method. The method can use the measuredWidth/height of its children or assign a new width/height by calling layout method of the view.

    lets say the onLayout method calls childview.layout(0,0,150,150) now the width/height of the view is different than the measured width/height.

    I would suggest not to use the measuredWidth/height outside onLayout method.

    to summarize .

    1. onMeasure -> sets up measuredWidth/measuredHeight
    2. onLayout -> sets up the width/height of the widget.

    additionallly
    public void View.layout(int l, int t, int r, int b)
    seems to be place where the assignment of position and size happens.

    0 讨论(0)
提交回复
热议问题