Android How to create a View Programmatically

笑着哭i 提交于 2020-01-29 07:53:44

问题


I need to create a view programmatically. The controls(buttons, labels, etc.) and their positions are known only at runtime.

Here I found "Defining a Layout Programmatically": http://mobile.tutsplus.com/tutorials/android/android-layout/

but I need to be able to draw components at exact locations so LinearLayout, TableLayout etc. doesn't work for me.

How can I do this?


回答1:


Create a custom ViewGroup; this allows you to manage placements of each of the children you have (buttons, labels, etc) manually. Check this vid from the google IO event in 2009 titled 'Make your Android UI Fast and Efficient', specifically from about 43 minutes onward for more details.




回答2:


I would take a look at the Lunar lander example in the android API samples here http://developer.android.com/resources/samples/LunarLander/index.html. This helped me out tremendously when starting out making games and having to dynamically draw to the screen.



来源:https://stackoverflow.com/questions/6896034/android-how-to-create-a-view-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!