When do I need inflaters?
I'm reading the book 'Hello, Android'. In the Sudoku example, it uses a options menu. It needs a MenuInflater that we use to read the menu definition from XML and turns it into a real view. To use button, textview, or many other views, I don't need to inflate them.
My question is, in what situations, I need inflaters? Why doesn't Android treat menus like other vie开发者_如何转开发ws?
You need an inflater at every place that you want to dynamically create a view out of an XML file.
Activity
layouts are automatically inflated when you call setContentView()
as they're always required.
But when the menu is required — which is only when the user first presses the Menu button — the XML-defined layout needs to be manually inflated.
Similarly, if you have a ListView
, you don't know in advance what rows will exist, so we have to inflate a View
from XML for each row in the list, as they're required.
Inflaters are mainly used for parsing Xml layout into view objects. As mentioned above the inflation is needed for making a link between the UI defined in the Xml for manipulating and making developer.
Whenever UI Updation is needed we need inflation and UI Updation is done through view object and developer can dynamically create view and add to existing view. Hence inflation helps developer to change behaviour of UI in xml layout according to specified condition in a program. With inflation, we are able define controllers in MVC for each xml layout where xml is view. Menu is also a view it has to inflated In certain code such setContentView(specifiedLayout) includes inflation But in earlier version it was not like this it was like setContextView(getInflater().inflate(specifiedLayout))
for ease of programming,android developers have incorporated inflation in setContentview() and there are lot scenarios like add view to layout addView(),etc..In most cases inflation has incorporated in code that why most of beginner does know inflation concept and has difficulties in understanding inflation in android.
精彩评论