有时候我们知道一个图片的文件名,我们需要知道在R文件中,该资源的ID,使用如下方法:

public static int getIdByName(Context context, String className, String resName) {
        String packageName = context.getPackageName();
        int indentify = context.getResources().getIdentifier(resName, className, packageName);
        return indentify;
    }

eg:

getIdByName(MainActivity.this, "drawable", "pic")

  上面一段代码可以获取drawable目录下,pic文件的id

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-15
  • 2021-10-19
猜你喜欢
  • 2022-12-23
  • 2021-08-06
  • 2022-12-23
  • 2021-10-22
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案