android_通过资源名_获取资源:
备忘:
private fun getDrawableIDByName(context: Context, name: String): Drawable? {
val res = context.resources
val img = context.resources.getIdentifier(name, "mipmap", context.packageName)
val drawable = try {
res.getDrawable(img, null)
} catch (e: Resources.NotFoundException) {
null
}
return drawable
}
该代码段提供了一个在Android中通过资源名获取Drawable的方法。它使用Context的resources获取getIdentifier方法,传入资源名、类型(如mipmap)和应用包名来查找资源ID,然后尝试用Resources的getDrawable方法得到Drawable对象,如果资源不存在则返回null。
4257

被折叠的 条评论
为什么被折叠?



