一、使用xml的方式:

1、LayoutInflater:

这个类可以把xml表述的Layout解析为View,从而可以使addView()方法添加View。

2、LayoutInflater与findViewById的区别:

两者都是实例化某一个对象,不同的是findViewById是通过找xml布局文件下的一个具体的widget控件进行实例化,而LayoutInflater是找res/layout 下的xml布局文件来实例化的。

3、使用方法:

LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);或
LayoutInflater inflater=LayoutInflater.from(Activity.this);或
LayoutInflater inflater=getLayoutInflater();

这三种方式本质上是相同的。

4、inflate():

使用inflate()可以将xml解析为View

inflaer.inflate(R.layout.xml文件名,parent);

 

范例:

android 动态加载LinearLayout的内容 android动态加载view_实例化

android 动态加载LinearLayout的内容 android动态加载view_android_02

1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     android:layout_width="wrap_content"
 3     android:layout_height="wrap_content"
 4     android:orientation="vertical" >
 5 
 6     <LinearLayout
 7         android:id="@+id/container"
 8         android:layout_width="wrap_content"
 9         android:layout_height="wrap_content" >
10     </LinearLayout>
11 
12 </LinearLayout>

main.xml

android 动态加载LinearLayout的内容 android动态加载view_实例化

android 动态加载LinearLayout的内容 android动态加载view_android_02

1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     android:layout_width="wrap_content"
 3     android:layout_height="wrap_content"
 4     android:orientation="vertical" >
 5 
 6     <TextView
 7         android:layout_width="wrap_content"
 8         android:layout_height="wrap_content"
 9         android:text="hello word" />
10 
11 </LinearLayout>

view.xml

android 动态加载LinearLayout的内容 android动态加载view_实例化

android 动态加载LinearLayout的内容 android动态加载view_android_02

1 public class Main extends Activity {
 2 
 3 @Override
 4  protected void onCreate(Bundle savedInstanceState) {
 5   super.onCreate(savedInstanceState);
 6   setContentView(R.layout.main);
 7   addView1();
 8   addView2();
 9   addView3();
10   addView4();
11  }
12 
13 private void addView1() {
14   LinearLayout container = (LinearLayout) findViewById(R.id.container);
15   LayoutInflater inflater = this.getLayoutInflater();
16   LinearLayout view = (LinearLayout) inflater
17     .inflate(R.layout.view, null);
18   container.addView(view);
19  }
20 
21 private void addView2() {
22   LinearLayout container = (LinearLayout) findViewById(R.id.container);
23   LayoutInflater inflater = LayoutInflater.from(this);
24   LinearLayout view = (LinearLayout) inflater
25     .inflate(R.layout.view, null);
26   container.addView(view);
27  }
28 
29 private void addView3() {
30   LinearLayout container = (LinearLayout) findViewById(R.id.container);
31   LayoutInflater inflater = (LayoutInflater) this
32     .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
33   LinearLayout view = (LinearLayout) inflater
34     .inflate(R.layout.view, null);
35   container.addView(view);
36  }
37 
38 private void addView4() {
39   LinearLayout container = (LinearLayout) findViewById(R.id.container);
40   LinearLayout view = this.getLayoutInflater().inflate(R.layout.view,
41     container);
42  }
43 
44 }

Main.java

 四个addView()方法本质上都是相同的。

 

二、使用java的方式:

android 动态加载LinearLayout的内容 android动态加载view_实例化

android 动态加载LinearLayout的内容 android动态加载view_android_02

1 private void addViewByJava() {
 2     LinearLayout container = new LinearLayout(this);//主布局container
 3     TextView tv = new TextView(this);//子View TextView
 4     // 为主布局container设置布局参数
 5     LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
 6       LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
 7     container.setLayoutParams(llp);//设置container的布局
 8     container.setOrientation(LinearLayout.HORIZONTAL);// 设置主布局的orientation
 9     // 为子View设置布局参数
10     ViewGroup.LayoutParams vlp = new ViewGroup.LayoutParams(
11       ViewGroup.LayoutParams.WRAP_CONTENT,
12       ViewGroup.LayoutParams.WRAP_CONTENT);
13     tv.setLayoutParams(vlp);// 设置TextView的布局
14     tv.setText("hello word");
15     container.addView(tv);// 将TextView 添加到container中
16    }