Create an ImageView dynamically and create an imageview

Source: Internet
Author: User

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 }});

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.