1.布局文件
<LinearLayout
android:id="@+id/viewGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical" />
2.代码
ViewGroup group = (ViewGroup) findViewById(R.id.viewGroup);
ImageView[] imageViews = new ImageView[images.length];
for (int i = 0; i < imageViews.length; i++) {
ImageView imageView = new ImageView(this);
imageView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
imageViews[i] = imageView;
imageView.setImageResource(R.drawable.ic_launcher);
group.addView(imageView);
}
这样就可以在不知道图片数量的时候,根据获取到的数量,动态创建ImageView。