2011-05-04 64 views

回答

0

假设您在main.xml中有LinearLayout

onCreate方法,你将有:

MyView mView = new MyView(this); 
mView.setId(ID_OF_YOUR_CUSTOM_VIEW); 
//modify next line as needed 
LayoutParams layoutParams = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); 
mView.setLayoutParams(layoutParams); 
LinearLayout layout = (LinearLayout) findViewById(R.id.ID_OF_YOUR_LAYOUT); 
layout.addView(mView); 
2

简短的回答:像任何其他View,使用setContentView(View v),虽然你可能会想将它添加到ViewGroup第一(通常的一个许多布局类)。

长答案:与其回答每个问题的基础知识,我相信社区可以为您做的最好的工作就是向您提供学习android基本编程的资源。最好的来源之一是android.com网站本身。请花点时间阅读Android开发者指南(链接:http://developer.android.com/guide/topics/fundamentals.html)。

此外,这里还有几个问题中,这样的链接很多资源来学习:

另外,为了避免在您的问题中得到相近的选票,请花点时间阅读FAQthese tips