private int getIdFromR(String strId, String type, String packageName) {
Resources resources = getResources();
int id = resources.getIdentifier(strId, type, packageName);
return id;
Resources resources = getResources();
int id = resources.getIdentifier(strId, type, packageName);
return id;
}
1.其中 strId:资源name,控件id(@+id/txtview).
2.type:id,drawable,layout等
3.packageName:R.java文件的包名
通过返回的id,然后得到控件或者layout.
本文介绍了一种在Android中从字符串资源名获取资源ID的方法。该方法通过解析资源名称、类型及包名来动态获取对应的资源标识符,适用于获取控件ID、布局文件等资源。

1808

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



