我编写了这个代码,用于将图像加载到ImageView小部件中:
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();
}
现在,我想加载几张图片。为此,我需要动态创建图像视图,但我不知道如何...
我想在for循环中运行我的代码:
for(int i;i<range;i++){
//LOAD SEVERAL IMAGES. READ URL FROM AN ARRAY
}
我的主要问题是动态地在循环中创建几个ImageView
答案 0 :(得分:22)
你可以根据你的要求修改布局,图像资源和图像(也可以是动态的)......
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);
}
答案 1 :(得分:1)
您可以使用此代码创建ImageViews
ImageView image = new ImageView(this);
LinearLayout.LayoutParams vp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
image.setLayoutParams(vp);
image.setMaxHeight(50);
image.setMaxWidth(50);
// other image settings
image.setImageDrawable(drawable);
theLayout.addView(image);
其中theLayout是您要将图片视图添加到的布局。
要进行更多自定义,请查看dev页面,其中列出了所有可能的选项。
答案 2 :(得分:0)
答案 3 :(得分:0)
试试这个
rootLayout = (LinearLayout) view1.findViewById(R.id.linearLayout1);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.CENTER_VERTICAL;
params.setMargins(0, 0, 60, 0);
for(int x=0;x<2;x++) {
ImageView image = new ImageView(getActivity());
image.setBackgroundResource(R.drawable.ic_swimming);
rootLayout.addView(image);
}