0

我想在列表视图中显示图像,其中图像存储在 drawble 文件夹中,我试图通过 XML 阅读这里是我的 XML 代码。

<music>
<song>
    <title>Live Shows</title>
    <thumb_url>@drawable/chat.png</thumb_url>
</song>
<song>
    <title>Space Bound</title>
    <thumb_url>@drawable/email_open.png</thumb_url>
</song>

请帮助我,我是 android 新手。

4

3 回答 3

1

解析 xml 并调用下面给出的函数

// imagePath - value of <thumb_url>@drawable/chat.png</thumb_url>
// imageView - view where image needs to be set
private void setImage(String imagePath,ImageView imageView)
{  

 String uriFromXML = imagePath;
 String suffix = ".png";
if ((uriFromXML != null) && (uriFromXML.startsWith("@")) && (uriFromXML.endsWith(suffix))) 
{
  int length = uriFromXML.length();
  int suffixLength = suffix.length();
  String uri = uriFromXML.substring(1, length - suffixLength);



  int imageResource = getResources().getIdentifier(uri, null,getPackageName());

  Drawable image = getResources().getDrawable(imageResource);
  imageView.setImageDrawable(image);
}
}
于 2012-09-18T06:48:51.227 回答
0

好的,我想你正在使用 androidhive tut。在惰性适配器类中将此代码更改imageLoader.DisplayImage(song.get(CustomizedListView.KEY_THUMB_URL), thumb_image); thumb_image.setImageResource(song.get(CustomizedListView.KEY_THUMB_URL));

于 2012-09-18T03:48:08.860 回答
0

试试这个,在 Xml

<music>
<song>
    <title>Live Shows</title>
    <thumb_url>chat</thumb_url>
</song>
<song>
    <title>Space Bound</title>
    <thumb_url>email_open</thumb_url>
</song>

并且在活动中,

int redId = getResources().getIdentifier("<your_package>:drawable/<resource_name>", null, null);
imageView.setBackgroundDrawable(getResources().getDrawable(resId));

where 会是类似的东西,com.example.package并且<resource_name>thumb_url从你的 xml 中填充。

参考文献:Ref1 Ref2

于 2012-09-18T05:38:26.613 回答