Android根据图片名字获取图片ID

/**
 * 根据图片名字反射出图片ID
 * @return
 */
public static int getImageID(String name){

    int id = -1;
    try {
        Field field=R.mipmap.class.getDeclaredField(name);
        String str=field.get(null).toString();
        id=Integer.parseInt(str);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return id;
}

猜你喜欢

转载自blog.csdn.net/qq_27400335/article/details/79899247