setContentView(R.layout.activity_main); //获得id为ll的线性布局 LinearLayout layout=(LinearLayout)this.findViewById(R.id.ll); //创建imageView的组件 final ImageView imge=new ImageView(MainActivity.this); imge.setImageResource(images[currentImage]); //将imageView组件添加到id=ll的线性布局里去 layout.addView(imge); //注册事件监听器 imge.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub } });
ps
之前定义了图片数组
int []images=new int[] { R.drawable.list1, R.drawable.list2, R.drawable.list3, R.drawable.list4, R.drawable.list5, R.drawable.list6, R.drawable.list7, R.drawable.list8, }; int currentImage=0;