简体   繁体   English

Android如何以编程方式创建视图

[英]Android How to create a View Programmatically

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/ 在这里,我找到了“以编程方式定义布局”: 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. 但我需要能够在确切的位置绘制组件,因此LinearLayout,TableLayout等对我不起作用。

How can I do this? 我怎样才能做到这一点?

Create a custom ViewGroup ; 创建自定义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. 从2009年标题为“让您的Android UI快速高效”的google IO事件中查看视频,特别是从大约43分钟开始,以获取更多详细信息。

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 . 我将在http://developer.android.com/resources/samples/LunarLander/index.html中查看android API示例中的Lunar lander示例。 This helped me out tremendously when starting out making games and having to dynamically draw to the screen. 当开始制作游戏并且必须动态绘制到屏幕时,这极大地帮助了我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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