Does posting Runnable to an UI thread guarantee that layout is finished when it is run?

后端 未结 1 1352
生来不讨喜
生来不讨喜 2020-12-08 00:42

First thing! I do know about ViewTreeObserver.onGlobalLayoutListener.

What made me ask this question is the following notice on Android

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

    I like the question too. It forced me to dig into Android source code once again. I believe this works because post() gets called after setContentView().

    Method setContentView() ends up in calling ViewGroup.addView() of the top view, and addView() call always triggers requestLayout(). In turn, requestLayout() posts a task to the main thread to be executed later. This task will execute measure and layout on the view hierarchy. Now if you post another task it will be put into the queue after layout task and, as the result, always executed after measure and layout happen. Thus you will always have valid sizes.

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