2

現在整数形式の動的文字列形式のR.drawableを使用したいのですが、どうすればよいですか。解決するには、このスニペットを案内してください。ありがとうございます。

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

2 に答える 2

13

あなたはこれを使うことができます

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

また

String imageName = "picture";
int resID = getResources().getIdentifier(imageName, "drawable", "package.name");
ImageView image;
image.setImageResource(resID);
于 2012-10-29T04:40:05.427 に答える
10

あなたはこれを使うことができます

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;
}
于 2012-10-29T04:57:35.003 に答える