问题
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"
回答1:
Issues to realise:
Code cleanliness-> Doing anything more than basic layouts in code can get very messy, very fast.
Code re-use-> It’s extremely easy to inflate an XML layout into a specified view with one or two lines of code
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.”
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.
来源:https://stackoverflow.com/questions/8544380/should-i-create-child-textviews-statically-or-dynamically