Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
741 views
in Technique[技术] by (71.8m points)

performance - Android - Declarative vs Programmatic UI

Has anyone seen or compiled benchmarks comparing declarative (XML) versus programmatically created UI's in Android?

There are things that Google has done to speed up the declarative approach, but you still do have the layout inflation step done at runtime.

Have you ever switched (or considered) changing your UI from declarative to programmatic for any reason?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Very little of the layout inflation is done at runtime. As hinted in the LayoutInflator API docs:

For performance reasons, view inflation relies heavily on pre-processing of XML files that is done at build time. Therefore, it is not currently possible to use LayoutInflater with an XmlPullParser over a plain XML file at runtime

If you take a look at the source, many of the views are pulled from a hash map based on their XML tag.

In answer to your question of whether I have benchmarked the inflater, I have to say no. Personally I find the idea of benchmarking the layout inflater in Android for your app to be the equivalent of benchmarking the DOM parser in Firefox for your website. I don't think the exercise is pointless, but you should have a much better reason than "my activity layout is too complicated for the inflater"...

If you require a dynamically generated layout, you would be best off creating it programmatically. If your view is simply taking a long time to inflate, you should simplify your view XML.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...