Android:setContentView和LayoutInflater [英] Android: setContentView and LayoutInflater

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

问题描述

setContentViewLayoutInflater有什么区别? 以及为什么在自定义toast中使用inflater和在自定义alertbox中使用setContentView的原因是什么?

What is the difference between setContentView and LayoutInflater? And whats the reason that we use inflater in custom toast and setContentView in custom alertbox?

推荐答案

您需要先了解一些内容,

You need to understand few things before,

Android中,每个Activity都有一个ViewRoot,通常是一个Window.但是,SurfaceView有其自己的窗口.因此,如果Activity具有SurfaceView,则它将具有多个Window.

In Android ,each Activity has one ViewRoot and usually one Window ,attached to it. However, a SurfaceView has its own window. So, if an Activity has a SurfaceView it will have more than one Window.

此活动用于占据整个Window的屏幕显示.视图已附加到此窗口.每个窗口都有一个Surface,Surface使用Canvas在该表面上绘制.视图附加到的窗口拥有该表面.

This activity is used for screen display occupying the entire Window. Views are attached to this Window. Every Window has a Surface and Surface uses Canvas to draw on the surface.The window the view is attached to owns the surface.

基本上ViewRoot负责收集和分发输入,而View负责管理焦点/键事件,Canvas仅负责使用onDraw()进行绘制"操作.

Basically ViewRoot is responsible for for collecting and dispatching the input and View is responsible for managing focus/key events, Canvas is only responsible for "drawing" operation using onDraw().

setContentView(View)是专门用于Activity的方法.在内部它调用WindowsetContentView(View).此方法将活动内容设置为显式视图.该视图直接放置在活动的视图层次结构中.从这一点开始,调用此函数将锁定"无法更改的窗口的各种特征.因此,它仅被调用一次.

setContentView(View) is a method exclusively available for Activity. Internally it calls the setContentView(View) of Window. This method sets the activity content to an explicit view. This view is placed directly into the activity's view hierarchy. Calling this function "locks in" various characteristics of the window that can not, from this point forward, be changed. Hence it is called only once.

LayoutInflater用于将布局XML文件实例化为其对应的View对象.基本上,目的是根据需要在运行时创建视图对象. 最好的示例是AdapterViews,例如ListViewSpinner等,其中根据记录数在运行时创建与单个记录对应的单个视图对象.

LayoutInflater is used to instantiate layout XML file into its corresponding View objects. Basically the purpose is to create view objects at runtime depending on the requirement. Best example is the AdapterViews like ListView, Spinner etc, where a single view object corresponding to single record is created at run time depending on the number of records.

Toast的情况下,如果要动态更改子视图,则使用LayoutInflater.在运行时更改图像.如果不对子视图进行任何更改,那么简单地将toast的setView(View)足以设置toast的布局视图.

In case of Toast, LayoutInflater is used if the child view is going to be altered dynamically eg. changing the image at run time. If no changes to child views are to be made then simplly setView(View) of toast is enough to set the layout view for toast.

如果仔细观察,与Toast相同的是AlertDialog.

Same as Toast is with the AlertDialog if you observe carefully.

希望它对您有帮助.

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

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