我需要一种方式来运行一些code在该活动满载的确切时刻,奠定了出来,绘制并准备针对用户的触摸控制。哪种方法/监听器做呢?
I need a way to run some code at the exact moment in which the activity is fully loaded, layed out, drawn and ready for the user's touch controls. Which method/listener does that?
Commonsware是正确的,没有解释你正在尝试做什么,为什么,这是不可能回答你的问题,我怀疑,有细节,你可能在想关于它的错误的方式。
Commonsware is right, without explaining what your are trying to do and why, it's not possible to answer your question and I suspect, with detail, you are probably thinking about it the wrong way.
不过,我也有,我需要做一些非常时髦的布置的东西都已经被测量过了一段code。
However, I do have some code where I needed to do some very funky layout stuff after everything had been measured.
我可以在布局扩展每个视图类和重写 onMeasure()
但是这将是一个大量的工作。于是,我终于实现了这一点。不是很大,但它的工作原理。
I could have extended each of the view classes in the layout and overriden onMeasure()
but that would have been a lot of work. So, I ended up doing this. Not great, but it works.
mainMenuLayout是我需要得到时髦的布局。该 onGlobalLayout
回调被调用时,布局已完成图纸。 Utils.setTitleText()
是funkiness发生,当我通过mainMenuLayout给它,它可以访问所有的子视图的位置和大小。
mainMenuLayout is the layout I needed to get funky with. The onGlobalLayout
callback is called when the layout has completed drawing. Utils.setTitleText()
is where the funkiness takes place and as I pass mainMenuLayout to it, it has access to the position and size of all of the child views.
mainMenuLayout.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
// only want to do this once
mainMenuLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
// set the menu title, the empty string check prevents sub-classes
// from blanking out the title - which they shouldn't but belt and braces!
if (!titleText.equals("")){
Utils.setTitleText(_context,mainMenuLayout,titleText);
}
}
});