2014-02-25 127 views
0

我想投,ImageViewRelativeLayout。因爲RelativeLayout是父視圖,並且在此有許多圖像作爲子視圖。所以我寫了代碼來下載圖像並將其設置爲圖像視圖。但在這裏,我想要將此下載的圖像設置爲RelativeLayout的父視圖的背景。Android:有沒有什麼辦法可以將ImageView投射到RelativeLayout

RelativeLayout layout = (RelativeLayout) findViewById(R.id.bg_image); 
      ImageView bgImage=(ImageView) findViewById(R.id.bground_image); 

downloader = new ImageDownloader(); 
     downloader.download(item.imageurl(), bgImage); 

///圖像現在在bgImage // 設置/////////現在在這裏,我想bgImage被設置爲佈局////

thankx的背景提前

回答

0

不,這是不可能的,因爲這些是完全不同的類型。

您可以將任何RelativeLayout或ImageView強制轉換爲視圖,因爲這是所有視圖的基類。

但是你想要做的應該有點不同。您可以從RelativeLayout中膨脹ImageView,因爲它是父級。

我會在稍後添加一段代碼。

更新:

// First inflate your layout 
View layout = (View)findViewById(R.layout.relativelayout); 
// Then inflate the imageview from the layout 
ImageView imgview = (ImageView)layout.findViewById(R.id.bground_image); 

// Then do your download logic with the imageview 
downloader = new ImageDownloader(); 
downloader.download(item.imageurl(), imgview); 

讓我知道,如果這個工程)。

+0

是的,你是正確的,謝謝,請添加代碼片段,以獲得一個想法。 –

+0

我的問題目前還不清楚。我想將下載的圖像設置爲'RelativeLayout'的背景圖像,而不是'ImageView'的背景圖像。 –

+0

好吧,您正在向下載程序提供ImageView類型變量。然後確保它接受一個RelativeLayout,然後在你的下載器中執行邏輯。 –

2

設置,你可以使用類似

我覺得是需要一個有相同尺寸的RelativeLayout另一個ImageView的(可以填父母)與 scaletype fitxy並設置圖像
layout.setBackground(new BitmapDrawable(<your image>)); 
+0

bgImage不是一種資源,所以它不會工作 –

+0

您也可以使用BitmapFactory製作的任何Bitmap(它可以解碼來自文件或其他流的jpg,png) –

0

最佳方式的背景圖像因爲你正在做懶惰loding和無法通過該相對佈局到加載器.......這個fitxy將作爲父母佈局的背景

或者另一種方式是改變你的downloder代碼接受相對佈局和設置背景一旦你有圖像

0

只需將圖像添加到RelativeLayout的背景。它應該是第一個,以便在佈局中的所有其他視圖之後。然後,您可以將其與預計使用ImageView的代碼一起使用。

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <ImageView android:id="@+id/an_image" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"/> 

    <!--other views go here--> 

</RelativeLayout> 

而且不,你不能在這些類型之間進行轉換,很明顯。這是Java!

相關問題