实现android:src ="@ drawable/image"在Android中以编程方式 [英] implement android:src="@drawable/image" programmatically in Android

查看:363
本文介绍了实现android:src ="@ drawable/image"在Android中以编程方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在图像按钮上设置前景图像.经过研究,我遇到了以下代码示例:

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"/>

我的查询是如何在代码中实际实现android:src.

My query is how to actually implement android:src in code.

推荐答案

尝试一下:

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

其中newimage drawable 文件夹中的图像名称.

where newimage is the image name in drawable folder.

已编辑
试试这个:

EDITED
try this:

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

其中 bm 是从服务器提取的位图.

where bm is bitmap extracted from server.

再次编辑
我看到您收到了Drawable;好吧,这样做:

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

这篇关于实现android:src ="@ drawable/image"在Android中以编程方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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