你可以轻松地做到这一点,你只需要打破它。首先加载要插入动态视图的布局。然后,您可以充实您的子视图,并根据需要填充多次。然后将视图添加到父版本,最后将活动的内容视图设置为父视图。
这里有一个例子:
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout parent = (LinearLayout) inflater.inflate(R.layout.main,
null);
for (int i = 0; i < 3; i++) {
View custom = inflater.inflate(R.layout.custom, null);
TextView tv = (TextView) custom.findViewById(R.id.text);
tv.setText("Custom View " + i);
parent.addView(custom);
}
setContentView(parent);
这里是我插入的main.xml文件:
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
这里是我放大,填充和动态插入的custom.xml视图:
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
希望这个例子有所帮助!