简体   繁体   English

性能:以编程方式生成的Android视图与xml视图

[英]Performance: Android views generated programmatically vs xml views

Suppose I want to inflate 50 views inside my fragment/activity/app programmatically. 假设我想以编程方式在我的片段/活动/应用程序中填充50个视图。

Compared to xml, does generating views programmatically in Android affect the performance of that particular fragment/activity/app? 与xml相比,Android中以编程方式生成视图是否会影响特定片段/活动/应用的性能?

If no then would there be any visible effect? 如果否,那会有什么明显的影响? If yes then can you please explain why? 如果是,那么您能解释一下原因吗?

The performance is not affected, but each has its pros and cons. 性能不会受到影响,但是每个都有优点和缺点。 Just to name a few: 仅举几个:

XML XML

Pros 优点

  1. The UI separated from code UI与代码分开
  2. You can reuse layouts, combine them 您可以重用布局,将它们合并
  3. Faster development, you can check the UI in preview, without rebuilding the app 开发速度更快,您可以在预览中检查UI,而无需重建应用程序

Cons 缺点

  1. Its static 它的静态

Programatically 编程方式

Pros 优点

  1. React to runtime conditions 对运行时条件做出反应

Cons 缺点

  1. more code (eg harder to maintain your code, more potential bugs) 更多代码(例如,难以维护代码,更多潜在错误)

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

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