gpt4 book ai didi

android - 当 Activity 完全布局并准备好进行用户交互时,会调用哪个方法?

转载 作者:IT老高 更新时间:2023-10-28 22:14:11 25 4
gpt4 key购买 nike

我需要一种方法来在 Activity 完全加载、布局、绘制并为用户触摸控件做好准备的那一刻运行一些代码。哪个方法/监听器会这样做?

最佳答案

Commonsware 是对的,如果不解释您要做什么以及为什么,就无法回答您的问题,而且我怀疑,详细地说,您可能以错误的方式思考它。

但是,我确实有一些代码需要在测量完所有内容后做一些非常时髦的布局。

我本可以扩展布局中的每个 View 类并覆盖 onMeasure() 但这将是很多工作。所以,我最终这样做了。不是很好,但很有效。

mainMenuLayout 是我需要的布局。 onGlobalLayout 回调在布局完成绘制时调用。 Utils.setTitleText() 是有趣的地方,当我将 mainMenuLayout 传递给它时,它可以访问所有 subview 的位置和大小。

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);
}

}
});

关于android - 当 Activity 完全布局并准备好进行用户交互时,会调用哪个方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10302803/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com