设置图像/背景源动态 [英] Set image / background source dynamically

查看:182
本文介绍了设置图像/背景源动态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能来动态设置背景图片?!

is there any possibility to set background image dynamically?!

我试着解释一下我的意思。

I try to explain what I mean.

String picVariable = getPictureFromServer();

ImageView image = (ImageView)v.findViewById(R.id.dynamic_image);

// I know, that doesn't work, but that's the way I looking for
image.setBackgroundResource(picVariable); 

感谢你在前进,

穆尔

诗。 我也看了这篇文章。这将在一个答案建议,使用Java反射的名字来获得R类的字段。但我从来没有使用过reflextion。一个例子是非常有益的

Ps. I also read this article. It would suggested in one answer, to use java reflection to get a field of the R class by name. But I've never used reflextion before. An example would be very helpfull

推荐答案

有时候,我应该采取更多的时间进行搜索:)

Sometimes I should take a bit more time for searching :)

我找到了答案阅读这篇文章,并能正常工作对我来说:

I found the answer reading this article, and it works fine for me:

// The server says, it should be *.png
String picName = getPictureFromServer();
picName = picName.replace(".png", "");

Resources r = getResources();
int picId = r.getIdentifier(picName, "drawable", "com.mypackage.myapp");

ImageView image = (ImageView)v.findViewById(R.id.dynamic_image);
image.setBackgroundResource(picId);

这篇关于设置图像/背景源动态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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