Android 利用addView 动态给Activity添加View组件
本文主要讲述如何动态给UI界面添加布局和控件,在编程的时候很多时候需要动态显示一些内容,在动态添加View的时候,主要使用addView方法。 1. addView方法简介 在Android 中,可以利用排版View的 addView 函数,将动态产生的View 物件加入到排版View 中。 例子如下: Activity代码: public class helloWorld extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView( R.layout.main ); // 取得LinearLayout 物件 LinearLayout ll = (LinearLayout)findViewById(R.id.viewObj); // 将TextView 加入到LinearLayout 中 TextView tv = new TextView(this); tv.setText(Hello World); ll. addView ( tv ); // 将Button 1 加入到LinearLayout 中 Button b1 = new Button(this); b1.setText(取消); ll.