使用getIdentifier()方法可以方便的獲各應用包下的指定資源ID。
主要有兩種方法:
(1)方式一
Resources resources = context.getResources();
int indentify = resources.getIdentifier(org.loveandroid.androidtest:drawable/icon",null,null);
if(indentify>0){
icon = resources.getDrawable(indentify);
}
(2)方式二
Resources resources = context.getResources();
int indentify= getResources().getIdentifier("icon", "drawable", "org.anddev.android.testproject");
主要有兩種方法:
(1)方式一
Resources resources = context.getResources();
int indentify = resources.getIdentifier(org.loveandroid.androidtest:drawable/icon",null,null);
if(indentify>0){
icon = resources.getDrawable(indentify);
}
-
第一個參數格式是:包名 + : + 資源文件夾名
+ / +資源名;是這種格式 然後其他的可以爲null
(2)方式二
Resources resources = context.getResources();
int indentify= getResources().getIdentifier("icon", "drawable", "org.anddev.android.testproject");
- 第一個參數爲ID名,第二個爲資源屬性是ID或者是Drawable,第三個爲包名。