Ответ 1
Вы можете легко сделать это, вы просто должны сломать это. Сначала вы загружаете макет, в который хотите вставить ваши динамические представления. Затем вы наполняете свое подпредставление и заполняете его столько раз, сколько вам нужно. Затем вы добавляете представление в родительский макет и, наконец, устанавливаете представление содержимого действия в родительское представление.
Вот пример:
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, который я вставляю в:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
</LinearLayout>
и вот представление custom.xml, которое я раздуваю, заполняю и динамически вставляю:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>