Android中動態添加View的兩種方法


一、使用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);

 

范例:

 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
 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
 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的方式:

 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    }
View Code

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM