2012-03-21 97 views
0

我在RelativeLayout中插入了一個ImageView。在這個ImageView之上,我試圖插入一個進度條,這個進度條在圖像下載後會隱藏。但是,當我添加ImageView後添加進度條時,它給了我一個錯誤 -如何在RelativeLayout中動態重疊視圖?

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

謝謝..

回答

1

您已經有了ProgressBar在佈局(你尋找它與findViewById),所以你不應該再添加它到佈局(如果它已經在佈局文件中,則與mRelativeLayoutRelativeLayout一樣)。刪除此行:

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

如果您有佈局的意見,你再次將它們添加到佈局!

+0

謝謝slukian!有效。 – FireAndIce 2012-03-21 07:15:53

0

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

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