简体   繁体   中英

How to convert R.drawable String to R.drawable integer format in android?

我想将R.drawable当前以动态字符串格式转换为整数格式。如何完成。请引导我用一个片段来解决它。谢谢。

Integer[] images={R.drawable.apple,R.drawable.mango,R.drawable.banana};

You can use this

String mDrawableName = "myimg";
int resID = getResources().getIdentifier(mDrawableName , "drawable", getPackageName());

or

String imageName = "picture";
int resID = getResources().getIdentifier(imageName, "drawable", "package.name");
ImageView image;
image.setImageResource(resID);

You can use this

public int[] getIntIds(Integer[] images){
    int[] temp = new int[images.length];
    String name;
    for(int i=0; i<= images.length; i++){
        name = getResources().getResourceEntryName(images[i]);
        temp[i] = getResources().getIdentifier(name , "drawable", getPackageName());
    }
    return temp;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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