活动完全布局并准备好与用户交互时会调用哪种方法? [英] Which method gets called the moment the activity is fully laid out and ready for user interaction?

查看:68
本文介绍了活动完全布局并准备好与用户交互时会调用哪种方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一种在活动被完全加载,布局,绘制并准备好用于用户的触摸控件的确切时间运行一些代码的方法.哪个方法/侦听器?

I need a way to run some code at the exact moment in which the activity is fully loaded, laid out, drawn and ready for the user's touch controls. Which method/listener does that?

推荐答案

Commonware是正确的,在没有解释您要做什么以及为什么的情况下,不可能回答您的问题,我怀疑您很可能在想关于它的错误方式.

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.

但是,我确实有一些代码,在对所有内容进行了测量之后,我需要做一些非常时髦的布局工作.

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()是最有趣的地方,当我将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);
                    }

                }
            });

这篇关于活动完全布局并准备好与用户交互时会调用哪种方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆