简体   繁体   English

Android - 将可绘制的ID存储在自定义XML文件中

[英]Android - Store drawable ids in a custom XML file

In my app I'm using an xml file to store some data, and images are stored in the drawable folder. 在我的应用程序中,我使用xml文件存储一些数据,图像存储在drawable文件夹中。 I would like to "link" these to, meaning that each object in the xml file to refer a certain image in the drawable folder. 我想“链接”这些,意味着xml文件中的每个对象都引用drawable文件夹中的某个图像。 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 你可以尝试只保存像“joe_pic”这样的名字,并且可以这样做

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 但如果您已经添加了“R.drawable.joe_pic”,则可以使用String.split获取第3个字符串部分

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM