setContentView 和 LayoutInflater 有什么区别? [英] What is the difference between setContentView and LayoutInflater?

查看:22
本文介绍了setContentView 和 LayoutInflater 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个包含多个片段的选项卡列表.我注意到,在主要活动中,我使用 setContentView 获取布局 xml 并使用 findViewById 获取相应的 UI 元素配置.

I am creating a tabs list with several fragments. I have noticed that, in the main activity, I used setContentView to get the layout xml and use findViewById to get the corresponding UI element config.

setContentView(R.layout.fragment_tabs);
mTabHost = (TabHost)findViewById(android.R.id.tabhost);
mTabHost.setup();
mTabManager = new TabManager(this, mTabHost, android.R.id.tabcontent);

但是,在不同的片段类中,我必须使用充气器.

However, in the different fragment class, I have to use the inflater instead.

View v = inflater.inflate(R.layout.webview, container, false);
WebView myBrowser=(WebView)v.findViewById(R.id.mybrowser);

而且这两个函数都是用来获取layout xml来创建对象的,为什么会有区别呢?第一个是在 onCreate 期间使用,第二个是在 onCreateView 期间使用吗?在什么情况下我应该选择其中之一?

And both function are used to get the layout xml to create an object, why is there a difference? Is the first one use during onCreate, and the second one during onCreateView? In what situation I should choose either of them?

推荐答案

setContentView 只是一个 Activity 方法.每个 Activity 都有一个 FrameLayout id "@+id/content"(即内容视图).您在 setContentView 中指定的任何视图都将成为该 Activity 的视图.请注意,您还可以将视图的实例传递给此方法,例如setContentView(new WebView(this)); 您使用的方法的版本会在幕后为您扩充视图.

setContentView is an Activity method only. Each Activity is provided with a FrameLayout with id "@+id/content" (i.e. the content view). Whatever view you specify in setContentView will be the view for that Activity. Note that you can also pass an instance of a view to this method, e.g. setContentView(new WebView(this)); The version of the method that you are using will inflate the view for you behind the scenes.

另一方面,Fragments 有一个名为 onCreateView 的生命周期方法,它返回一个视图(如果有的话).执行此操作的最常见方法是在 XML 中扩充视图并在此方法中返回它.在这种情况下,您需要自己充气.片段没有 setContentView 方法

Fragments, on the other hand, have a lifecycle method called onCreateView which returns a view (if it has one). The most common way to do this is to inflate a view in XML and return it in this method. In this case you need to inflate it yourself though. Fragments don't have a setContentView method

活动和视图都有一个名为 findViewById() 的方法.活动版本将在其内容视图中搜索具有给定 id 的视图(因此,在内部,它将调用 contentView.findViewById()).这意味着需要在 contentView 变得可用之前对其进行设置.像 setContentView 一样,片段没有 findViewById 的方法(这是有道理的,因为没有内容视图).只需使用 getView().findViewById() 代替相同的行为.

Activities and views both have a method called findViewById(). The activity version will search for a view with the given id inside of it's content view (therefore, internally, it will call contentView.findViewById()). This means that the contentView needs to be set before it becomes usable. Like setContentView, fragments don't have a method for findViewById (which makes sense, because there is no content view). Simply use getView().findViewById() instead for the same behaviour.

LayoutInflater.inflate 只是膨胀并返回一个视图(你可以在任何地方使用它).您仍然需要将该视图设置为 Activity 中的内容视图

LayoutInflater.inflate just inflates and returns a view (you can use this anywhere). You still need to set that view as the content view within an Activity

这篇关于setContentView 和 LayoutInflater 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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