Create an ImageView dynamically and create an imageview
1. layout File
<LinearLayout android:id="@+id/viewGroup" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_horizontal" android:orientation="vertical" />
2. Code
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);}In this way, you can dynamically create an ImageView Based on the obtained number when you do not know the number of images.
Why can't animations be added to dynamically created imageviews?
Let's talk about the animation of animation. I want to make a playing card game and use an ImageView object as an attribute of each playing card. Therefore, the ImageView object is dynamically created. Then some animation effects (such as licensing or something) want to implement animation on the ImageView of each playing card. Someone says no, I'm still trying...
How can I add a click listener to the dynamically created ImageView?
SetOnTouchListener is incorrect. Use setOnClickListener ivArrowfwd. setOnTouchListener (new OnTouchListener () {@ Override public boolean onTouch (View arg0, MotionEvent arg1) {System. out. println ("sdfsdf" + (ImageView) view. findViewById (id ). getTag (); return false; // returns true after your operation is completed }});