簡體   English   中英

如何動態地在RelativeLayout中重疊視圖?

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

我在RelativeLayout中插入了一個ImageView。 在此ImageView的頂部,我嘗試插入一個進度條,下載圖像后該進度條將不可見。 但是,當我在添加ImageView之后添加進度條時,它給我一個錯誤-

java.lang.IllegalStateException:指定的子代已經有一個父代。 您必須先在孩子的父母上調用removeView()。

這是代碼:

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

謝謝..

您已經在布局中有了ProgressBar (使用findViewById搜索它),因此您不應該再次將其添加到布局中(如果mRelativeLayout RelativeLayout已經存在於布局文件中,則與之相同)。 刪除以下行:

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

如果您在布局中具有視圖, 則無需將其再次添加到布局中!

您到底想對圖像和進度條做什么。 如果要在圖像上顯示ProgressBar,請使用FrameLayout。 在那您也可以使用VISIBLE和GONE的東西。

您想在哪里顯示動態生成的視圖? 因為我們確實有適配器來顯示具有不同內容的相同類型的數據。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM