简体   繁体   中英

How to overlap views in RelativeLayout, dynamically?

I've an ImageView inserted in the RelativeLayout. On top of this ImageView I'm trying to insert a progressbar, which would go invisible after the image is downloaded. But, when I add progressbar after adding the ImageView, it gives me an error -

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

Here is the code:

                mRelativeLayout = (RelativeLayout) mGallery.findViewById(R.id.relative_progress_spin_layout);
                RelativeLayout.LayoutParams relativeLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
                relativeLayoutParams.addRule(RelativeLayout.ALIGN_TOP, R.id.progress_spin);

                progressBar = (ProgressBar) mGallery.findViewById(R.id.progress_spin);


                image = new ImageView(GalleryModuleActivity.this);
                image.setPadding(4, 4, 4, 4);
                image.setScaleType(ImageView.ScaleType.FIT_XY);

                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(200, LinearLayout.LayoutParams.MATCH_PARENT);
                image.setLayoutParams(layoutParams);

                mRelativeLayout.addView(image);
                mRelativeLayout.addView(progressBar);
                mHorizontalLayout.addView(mRelativeLayout);

Thanks..

You already have the ProgressBar in the layout(you search for it with findViewById ) so you shouldn't add it again to the layout(the same thing with the mRelativeLayout RelativeLayout if it is already in the layout file). Remove this lines:

mRelativeLayout.addView(progressBar);
mHorizontalLayout.addView(mRelativeLayout);

If you have the views in the layout you don't add them again to the layout!

What you exactly want to do with images and progress bar. If you want to display ProgressBar on images use FrameLayout. In that also you can use VISIBLE and GONE stuff.

Where you want to display that dynamic generated views ? Because we do have adapter to display same type of data with different content.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM