Android中动态添加View

2020-07-20  本文已影响0人  cain07

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

二、使用java的方式:

private void addViewByJava() {
    LinearLayout container = new LinearLayout(this);//主布局container
    TextView tv = new TextView(this);//子View TextView
    // 为主布局container设置布局参数
    LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
      LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
    container.setLayoutParams(llp);//设置container的布局
    container.setOrientation(LinearLayout.HORIZONTAL);// 设置主布局的orientation
    // 为子View设置布局参数
    ViewGroup.LayoutParams vlp = new ViewGroup.LayoutParams(
      ViewGroup.LayoutParams.WRAP_CONTENT,
      ViewGroup.LayoutParams.WRAP_CONTENT);
    tv.setLayoutParams(vlp);// 设置TextView的布局
    tv.setText("hello word");
    container.addView(tv);// 将TextView 添加到container中
   }
上一篇 下一篇

猜你喜欢

热点阅读