自定义视图或膨胀布局?
背景信息:
我正在为自己和几个朋友开发一个小型应用程序,它包含一些带有基于自定义 XML 布局的项目的 ListView
。布局有点重 - 一些 ImageViews
、TextViews
、一个 LinearLayout
和一个 RelativeLayout
- 所以我开始思考关于性能。我个人没有遇到任何性能问题,但我知道我的一些将使用该应用程序的朋友没有高端手机,他们可能会对这些布局有问题。
我记得看过一个有关 Android 性能的视频演示,我记得 Romain Guy 提到自定义 View
比不断膨胀自定义 XML 布局更能提高性能。我相信上下文是 Gmail 应用程序的早期开发(我相信大约是 Android 1.5),其中 Listview
的每个项目都相当复杂。
可能值得指出的是,我在应用程序中重用了 Views
,并且我使用了 Google 推荐的 ViewHolder
原则。
我的问题:使用自定义视图
是否可以提高性能,或者可以扩充自定义XML布局吗?
Background information:
I'm working on a small app for myself and a few friends, and it contains a few ListViews
with items based on custom XML layouts. The layouts are somewhat heavy - a few ImageViews
, TextViews
, a LinearLayout
and a RelativeLayout
- so I started thinking about performance. I'm not personally experiencing any performance issues, but I know that some of my friends, who'll be using the app, don't have high-end phones and that they might have issues with these layouts.
I remember watching a video presentation regarding performance in Android, and I recall Romain Guy mentioning something about custom Views
being better for performance than constantly inflating custom XML layouts. I believe the context was the early development of the Gmail application (around Android 1.5, I believe), where each item of the Listview
was rather complex.
It's probably worth pointing out that I'm reusing Views
in my application, and that I'm using the ViewHolder
principle as recommended by Google.
My question: Is it better for performance to use custom Views
or is it OK to inflate custom XML layouts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
与往常一样,答案是“这取决于”——与代码中的简单创建相比,从 XML 中膨胀视图会带来一些性能损失(解析开销),但也提供了更大的灵活性。您绝对应该尽可能重用视图(尤其是在较大的列表中) - 它可以显着提高滚动性能
As always, then answer is "it depends" - inflating view from XML imposes some performance penalty ( parsing overhead ) over plain creation in code, but also provides greater flexibility. You should definitely reuse views whenever possible ( especially in larger lists) - it improves scrolling performance dramatically
如果您可以创建自定义布局而不是线性布局和相对布局,那么创建自定义布局对您来说会更有利。这有点像,您应该使用相对布局,而不是使用嵌套线性布局。如果您使用相对视图、线性视图和一大堆视图,那么只需编写自定义布局应该会有所帮助。
If you can create a custom layout instead of having a linear layouts and relative layouts it will be more beneficial for you to just create a custom layout. It's kind of like, instead of using nested linear layouts, you should just use a relative layout. If you use relative and linear and a whole bunch of views, then just coding a custom layout should be beneficial.