Android的 - 在自定义XML文件中存储绘制IDS [英] Android - Store drawable ids in a custom XML file
本文介绍了Android的 - 在自定义XML文件中存储绘制IDS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的应用我使用的XML文件来存储一些数据和图像存储在绘制文件夹中。我想链接到这些,意味着在XML文件中的每个对象在绘制文件夹是指某种形象。我怎么能acomplish呢?
谢谢!
<联系人和GT;
<联系与GT;
<名称>&乔LT; /名称>
<图片及GT; R.drawable.joe_pic< /图片及GT;
< /联系与GT;
<联系与GT;
<名称>院长及LT; /名称>
<图片及GT; R.drawable.dean_pic< /图片及GT;
< /联系与GT;
< /触点>
解决方案
您可以只保存名称,比如joe_pic先试后可以得到的这样
私人无效showImage(){
字符串URI =绘制/图标; // INT imageResource = R.drawable.icon;
INT imageResource = getResources()则getIdentifier(URI,空,getPackageName()); ImageView的ImageView的=(ImageView的)findViewById(R.id.myImageView);
。绘制的图像= getResources()getDrawable(imageResource);
imageView.setImageDrawable(图片);
}
但如果你已经添加了R.drawable.joe_pic完全可以使用String.split获得第三串部分
In my app I'm using an xml file to store some data, and images are stored in the drawable folder. I would like to "link" these to, meaning that each object in the xml file to refer a certain image in the drawable folder. How could i acomplish this ? Thanks!
<contacts>
<contact>
<name>Joe</name>
<picture>R.drawable.joe_pic</picture>
</contact>
<contact>
<name>Dean</name>
<picture>R.drawable.dean_pic</picture>
</contact>
</contacts>
解决方案
you can try with only saving the name like "joe_pic" and can get this way
private void showImage() {
String uri = "drawable/icon";
// int imageResource = R.drawable.icon;
int imageResource = getResources().getIdentifier(uri, null, getPackageName());
ImageView imageView = (ImageView) findViewById(R.id.myImageView);
Drawable image = getResources().getDrawable(imageResource);
imageView.setImageDrawable(image);
}
but if you have already added "R.drawable.joe_pic" complete can use String.split to get 3 rd string part
这篇关于Android的 - 在自定义XML文件中存储绘制IDS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文