简体   繁体   English

如何动态地在RelativeLayout中重叠视图?

[英]How to overlap views in RelativeLayout, dynamically?

I've an ImageView inserted in the RelativeLayout. 我在RelativeLayout中插入了一个ImageView。 On top of this ImageView I'm trying to insert a progressbar, which would go invisible after the image is downloaded. 在此ImageView的顶部,我尝试插入一个进度条,下载图像后该进度条将不可见。 But, when I add progressbar after adding the ImageView, it gives me an error - 但是,当我在添加ImageView之后添加进度条时,它给我一个错误-

java.lang.IllegalStateException: The specified child already has a parent. java.lang.IllegalStateException:指定的子代已经有一个父代。 You must call removeView() on the child's parent first. 您必须先在孩子的父母上调用removeView()。

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). 您已经在布局中有了ProgressBar (使用findViewById搜索它),因此您不应该再次将其添加到布局中(如果mRelativeLayout RelativeLayout已经存在于布局文件中,则与之相同)。 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. 如果要在图像上显示ProgressBar,请使用FrameLayout。 In that also you can use VISIBLE and GONE stuff. 在那您也可以使用VISIBLE和GONE的东西。

Where you want to display that dynamic generated views ? 您想在哪里显示动态生成的视图? Because we do have adapter to display same type of data with different content. 因为我们确实有适配器来显示具有不同内容的相同类型的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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