简体   繁体   English

Android ANR和XML布局与Java布局

[英]Android ANRs and XML layouts vs Java layouts

I have been struggling with a nasty ANR problem with my app, which loads a SurfaceView. 我一直在为应用程序加载一个SurfaceView时遇到棘手的ANR问题。 Originally I created it Java-wise in the Activity: 最初,我在Activity中以Java方式创建了它:

mSurfaceView msv = new mSurfaceView(this);
//set params etc.
myLayout.add(msv);

I then decided to switch to XML and lay out everything in an XML file and then just findViewById(...), and the ANR seems to have gone. 然后,我决定切换到XML,并将所有内容布置在XML文件中,然后只是findViewById(...),ANR似乎消失了。

My questions is: is XML loading faster than with Java? 我的问题是:XML加载比Java加载快吗?

The XML files are never read at runtime. 永远不会在运行时读取XML文件。 They are converted to a binary format when compiling. 它们在编译时会转换为二进制格式。 I assume there are some optimizations done during this conversion which makes inflating the layouts relatively fast. 我假设在此转换过程中进行了一些优化,从而使布局膨胀相对较快。 I would imagine XML layouts are faster than doing it in code. 我可以想象XML布局比在代码中更快。 XML layouts and programming the layout in code are not one to one, I've noticed bugs sometimes with XML that were solved by doing the layout in the Java code. XML布局和用代码对布局进行编程不是一对一的,我注意到XML有时会通过在Java代码中进行布局来解决一些错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM