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);
范例:
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/container"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</LinearLayout>
</LinearLayout>
view.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello word" />
</LinearLayout>
Main.java
public class Main extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addView1();
addView2();
addView3();
addView4();
}
private void addView1() {
LinearLayout container = (LinearLayout) findViewById(R.id.container);
LayoutInflater inflater = this.getLayoutInflater();
LinearLayout view = (LinearLayout) inflater
.inflate(R.layout.view, null);
container.addView(view);
}
private void addView2() {
LinearLayout container = (LinearLayout) findViewById(R.id.container);
LayoutInflater inflater = LayoutInflater.from(this);
LinearLayout view = (LinearLayout) inflater
.inflate(R.layout.view, null);
container.addView(view);
}
private void addView3() {
LinearLayout container = (LinearLayout) findViewById(R.id.container);
LayoutInflater inflater = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout view = (LinearLayout) inflater
.inflate(R.layout.view, null);
container.addView(view);
}
private void addView4() {
LinearLayout container = (LinearLayout) findViewById(R.id.container);
LinearLayout view = this.getLayoutInflater().inflate(R.layout.view,
container);
}
}
四个addView()方法本质上都是相同的。
二、使用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中
}