本文介绍了在循环中动态创建ImageViews的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我写了这个code,在ImageView的小部件加载一个图像:
I have written this code that loads an image to in ImageView widget:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gallery);
i = (ImageView)findViewById(R.id.imageView1);
new get_image("https://www.google.com/images/srpr/logo4w.png") {
ImageView imageView1 = new ImageView(GalleryActivity.this);
ProgressDialog dialog = ProgressDialog.show(GalleryActivity.this, "", "Loading. Please wait...", true);
protected void onPreExecute(){
super.onPreExecute();
}
protected void onPostExecute(Boolean result) {
i.setImageBitmap(bitmap);
dialog.dismiss();
}
}.execute();
}
BU现在,我想加载多个图像。为了这个,我需要动态地创建图像的意见,但我不知道如何...
bu now, I want to load several images. for this I need create image views dynamically but i don't know how...
我要运行我的code里面的一个循环:
I want run my code inside a for loop:
for(int i;i<range;i++){
//LOAD SEVERAL IMAGES. READ URL FROM AN ARRAY
}
我的主要问题是在循环中创建多个ImageViews动态
my main problem is creating several ImageViews inside a loop dynamically
推荐答案
您可以根据您的要求修改布局,图像资源和没有图像的(可能是动态的为好)...
you can modify the layout , image resource and no of images (may be dynamic as well) according to your requirement...
LinearLayout layout = (LinearLayout)findViewById(R.id.imageLayout);
for(int i=0;i<10;i++)
{
ImageView image = new ImageView(this);
image.setLayoutParams(new android.view.ViewGroup.LayoutParams(80,60));
image.setMaxHeight(20);
image.setMaxWidth(20);
// Adds the view to the layout
layout.addView(image);
}
这篇关于在循环中动态创建ImageViews的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!