我已经从网址下载了一个位图,现在我希望将其设置为我的相对布局的背景。 这是我的代码
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);
我该如何去做?感谢您的帮助
答案 0 :(得分:7)
您应该使用BitmapDrawable
var bitmapDrawable = new BitmapDrawable(bitmap);
...
relativelayout.SetBackground(bitmapDrawable);
//or
relativelayout.SetBackgroundDrawable(bitmapDrawable);
//SetBackgroundDrawable deprecated in API level 16
答案 1 :(得分:0)
Android的方式是:
relativelayout.setBackground(...);
表示int资源,或
relativelayout.setBackgroundResource(...);
为drawable。
我确定你能找到符合Xamarin的方法
答案 2 :(得分: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);**
答案 3 :(得分:0)
请尝试一次。
BitmapDrawable background = new BitmapDrawable(YOUR_BITMAP_SOURCE);
YOUR_LAYOUT.setBackground(background);