我应该静态或动态创建子textviews? [英] Should I create child textviews statically or dynamically?

查看:180
本文介绍了我应该静态或动态创建子textviews?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表视图,其中的项目包含不同数量的孩子textviews的。

  5分钟
最多20个

我试过两种方式,我只在VM来测试我的应用程序,所以我真的不能告诉任何区别表现明智的。

但什么是做到这一点的最好方法是什么?

我要创建我的XML 20 textviews,只是躲我不使用的人?
或者只是创建和添加新的textviews每次,导致没有鬼意见


解决方案

的问题来实现:


  1. code清洁 - >做任何事情比code基本布局更可以得到非常凌乱,非常快


  2. code再利用 - >这是非常容易膨胀的XML布局到指定的视图
    与code的一行或两行


  3. code效果 - >创建必要为IN- code布局的对象导致不必要的垃圾回收。根据Android的性能设计的文章,避免如果你能创造短期的临时对象。


  4. 属性的可用性 - >在XML布局定义视图暴露了并不总是可用通过对象的方法属性


可能的缺点:

这使得需要更多的时间做一个XML布局与在code定义布局,尤其是如果只有一个或需要被改变了两个界面元素。

想什么我要完成之后,它是有道理的,我使用XML布局我需要的动态视图变化。这将是比code的只有几行做我布局的变化,而无需使用XML布局的更多。

现在,您可以根据您的要求决定的。

I have a listview where the items contain different amount of child textviews.

min 5
max 20

i've tried both ways and I only have the vm to test my apps on so I can't really tell any difference performance wise.

but what's the best way to do this?

should I create the 20 textviews in my xml and just hide the ones I don't use? or just create and add new textviews everytime, resulting in no "Ghost views"

解决方案

Issues to realise:

  1. Code cleanliness-> Doing anything more than basic layouts in code can get very messy, very fast.

  2. Code re-use-> It’s extremely easy to inflate an XML layout into a specified view with one or two lines of code

  3. code Performance-> Creating the objects necessary for an in-code layout leads to unnecessary garbage collection. As per the Android Designing for Performance article, "avoid creating short-term temporary objects if you can."

  4. Attribute availability-> Defining Views in an XML layout exposes attributes that are not always available by object methods.

Possible disadvantages:

It make take more time to do an XML layout versus defining the layout in code, especially if there are only one or two interface elements that need to be changed.

After thinking about what I want to accomplish, it makes sense for me to use XML layouts for the dynamic view changes I need. It would be more than just a few lines of code to do my layout changes without utilizing XML layouts.

Now you can decide according to your requirements.

这篇关于我应该静态或动态创建子textviews?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆