2012-06-26 13 views
0

在我的應用程序中,我使用xml文件來存儲一些數據,並且圖像存儲在可繪製文件夾中。我想將它們「鏈接」到,這意味着xml文件中的每個對象都會引用可繪製文件夾中的某個圖像。我怎麼能這樣做? 謝謝!Android - 在可定製的XML文件中存儲drawable ID

<contacts> 
    <contact> 
     <name>Joe</name> 
     <picture>R.drawable.joe_pic</picture> 
    </contact> 
    <contact> 
     <name>Dean</name> 
     <picture>R.drawable.dean_pic</picture> 
    </contact> 
</contacts> 

回答

2

你可以只保存名稱,比如「joe_pic」先試後可以得到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); 
} 

,但如果你已經添加了「R.drawable.joe_pic」完全可以使用String.split來得到第三個字符串部分

相關問題