实现机器人:SRC =" @可绘制/图片"编程的机器人 [英] implement android:src="@drawable/image" programatically in Android

查看:169
本文介绍了实现机器人:SRC =" @可绘制/图片"编程的机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置前景色图像上的图像按钮。经过一番研究,我碰到这个code样品:<​​/ P>

I am trying to set the foreground image on an image button. After some research, I came across this code sample:

<ImageButton android:text="Button" android:id="@+id/button1"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:src="@drawable/icon"/>

我的查询是如何真正实现机器人:SRC在code。 我想AP preciate什么想法。

My query is how to actually implement android:src in code. I would appreciate any thoughts.

推荐答案

试试这个:

ImageButton btn = (ImageButton)findViewById(R.id.button1);
btn.setImageResource(R.drawable.newimage);

其中, newimage 是映像名称的绘制的文件夹。

where newimage is the image name in drawable folder.

EDITED
试试这个:

EDITED
try this:

ImageButton btn = (ImageButton)findViewById(R.id.button1);
btn.setImageBitmap(bm);

其中的 BM 的位图是从服务器中提取。

where bm is bitmap extracted from server.

EDITED AGAIN
我看到你收到的可绘制;好了,做到这一点:

EDITED AGAIN
I see you receive a Drawable; well, do this:

normalImage = Drawable.createFromStream(code);
Bitmap bm = ((BitmapDrawable)normalImage).getBitmap();
ImageButton btn = (ImageButton)findViewById(R.id.button1);
btn.setImageBitmap(bm);

这篇关于实现机器人:SRC =&QUOT; @可绘制/图片&QUOT;编程的机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆