什么时候在 Android 上创建 UI 元素? [英] When are UI elements created on Android?

查看:27
本文介绍了什么时候在 Android 上创建 UI 元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Android 视图是在什么时候创建的?活动什么时候开始?应用程序何时启动?当调用 setContentView() 时?还是在其他时间?

At which point are Android views created? When the activity starts? When the app is started? When a call to setContentView() is made? Or at some other point?

推荐答案

您的意思是什么时候获取视图的对象是安全的?

You mean when is it safe to get the objects of your view?

如果您引用活动,在 onCreate 方法中,在您调用 setContentView(...) 之后,您可以开始调用 findViewById(...) 引用视图的对象.

If you refer to Activities, in the onCreate method, after you call setContentView(...) you can start calling findViewById(...) to refer to the objects of your view.

在 Fragments 的情况下,视图是在 onCreateView(...) 方法中创建的.您可以在 onActivityCreated(...) 回调中引用视图,这是在创建 Activity 后调用的,因此在这里引用视图是安全的.

In the case of Fragments, the view is created in the onCreateView(...) method. You can refer to the view in the onActivityCreated(...) callback, this is called after the Activity has been created, so it is safe to refer to the view here.

这篇关于什么时候在 Android 上创建 UI 元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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