一、前导
ViewGroup是View的子类,ViewGroup通过addView()向自己的容器中添加View。而添加的这个View也可以是一个ViewGroup,这样,添加的这个ViewGroup又可以addView(),理论上可以一直这样一层一层的添加。
二、使用举例
注意:先addView(),然后才能调用view.findViewById()。
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:id="@+id/add"
android:clickable="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
<LinearLayout
android:id="@+id/container2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
add_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/container3"
android:orientation="vertical" >
<Button
android:id="@+id/add_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="add button" />
</LinearLayout>
add_view2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="@+id/add_btn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="add button2" />
</LinearLayout>
MainActivity
package com.example.testaddview;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.Toast;
public class MainActivity extends Activity {
private LinearLayout container, container2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
container = (LinearLayout) this.findViewById(R.id.container);
container2 = (LinearLayout) this.findViewById(R.id.container2);
this.findViewById(R.id.add).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.add_view, null);
// container2.addView(view);
container.addView(view);//2 methods are all useful.
final LinearLayout container3 = (LinearLayout) view.findViewById(R.id.container3);
view.findViewById(R.id.add_btn).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
View view2 = LayoutInflater.from(MainActivity.this).inflate(R.layout.add_view2, null);
container3.addView(view2);
view2.findViewById(R.id.add_btn2).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "click the button", 0).show();
}
});
}
});
}
});
}
}