什么时候机器人首先做的布局?什么时候、机器人、布局

2023-09-06 03:19:38 作者:我比想象中勇敢

我需要prepare基于部件和意见在我活动的初始布局某些对象。我想做到这一点,在初始化步骤,理想的onCreate和ONSTART。考虑改变这种情况发生的时候,我们是不是在前台的可能性,其中一些可能需要发生在onResume。

I need to prepare some objects based on the initial layout of widgets and views in my Activity. I'd like to do it in the initialization steps, ideally onCreate and onStart. Considering the possibility of changes that happen when we're not in the foreground, some of them might need to happen in onResume.

所以,我检查我是否可以衡量我的观点已经奠定了。

So I checked whether I could measure how my views had been laid out.

Log.d("MyApp", "w,h of X is " + findViewById(R.id.X).getWidth() +
      "," + findViewById(R.id.X).getHeight());

我的onCreate,ONSTART和onResume跑了这一点。每次我得到0,0的宽度,高度。如果我等待的onTouchEvent我得到的布局信息,这样的布局是由当时做的。

I ran this in onCreate, onStart, and onResume. Each time I get 0,0 for width, height. If I wait for onTouchEvent I get the layout information, so the layout is done by then.

我很惊讶,布局未设置,并最终由我看到onResume的时间。我预计小部件在已经奠定了,准备通过再乱动。

I'm surprised that the layout isn't set and final by the time I'm seeing onResume. I expected the widgets to be already laid out and ready to fiddle with by then.

推荐答案

布局由视图层次结构需要完成。

Layout is done by the view hierarchy as needed.

当你的应用程序开始,这将是在 onResume一段时间()(因为你的窗口正在长大,并放置在屏幕上)。

When your app is starting, this will be some time after onResume() (as your window is being brought up and placed on the screen).

要了解布局操作正确的方法是通过各种视图层次回调 - View.onSizeChanged()等在布局作为文件的这里:

The correct way to find out about layout operations is through the various view hierarchy callbacks -- View.onSizeChanged() etc as documented under "Layout" here: