简体   繁体   中英

Android varying number of textviews to display

For exmaple I have int variable "number" that is passed throughout the program

and if the number = 7 I want to create 7 identical textviews and create 7 IDs for each of them

if the number = 5 I want to create 5 textviews etc.

My guess is that I should not create any textviews in my xml file and rather create views in my java code . Is this the right approach?

I would do this with a XML holding the EditText and a ListView in another xml, then in the Adapter of that ListView you can inflate and instanciate the EditText XML items.

This way you stay much cleaner and it might be less work if you change style etc. You do not need to take care of a scrollview yourself for more items.

Also you can have items and ListView adapting to screendensity, screensize, orientation etc. without going through a hell of code - if this is a requirement.

If the last item in the list needs to be a button, you could inflate and instanciate such an item layout too in the Adapter.

如果您的布局在编译时未知(例如,不知道View的数量),那么您唯一的选择是以编程方式进行布局。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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