2015-09-07 108 views
0

我已经从网址下载了位图,现在我希望将其设置为相对布局的背景。 这是我的代码如何在相对布局背景中设置位图图像

Bitmap bitmap = null; 
    WebClient client = new WebClient(); 
    client.DownloadDataAsync (new Uri (mItems [position].Profile_Pic)); 
    client.DownloadDataCompleted += (se, res) => { 
     bitmap = BitmapFactory.DecodeByteArray (res.Result, 0, res.Result.Length); 
     var relativelayout=row.FindViewById<RelativeLayout>(Resource.Id.relativeLayout1); 

我如何去这样做的帮助表示感谢

回答

6

您应该使用BitmapDrawable

var bitmapDrawable = new BitmapDrawable(bitmap); 

... 

relativelayout.SetBackground(bitmapDrawable); 
//or 
relativelayout.SetBackgroundDrawable(bitmapDrawable); 
//SetBackgroundDrawable deprecated in API level 16 
0

Android的办法是:

relativelayout.setBackground(...); 

int资源,或

relativelayout.setBackgroundResource(...); 

为可绘制的。

我相信你能找到一个符合Xamarin方法

0
Just add a ImageView in your relativeLayout and get an instance of your imageView and try this :- 

In your axml file : 
<RelativeLayout 
    android:id="@+id/relativeLayout1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 
<ImageView 
      android:id="@+id/img" 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      android:visibility="visible" 
      android:fitsSystemWindows="true" 
      android:adjustViewBounds="true" 
      android:scaleType="matrix" 
      android:background="@drawable/image_placeholder" 
      android:padding="3dp" /> 
</RelativeLayout> 

In Code: 

var imageView =row.FindViewById<ImageView>(Resource.Id.img); 
**img.SetImageBitmap(bitmap);**