有時候因為程式的需求會需要用檔名來取得Resource id
import java.lang.reflect.Field;
public int getResId(String variableName, Class<?> c) {
try {
Field idField = c.getDeclaredField(variableName);
return idField.getInt(idField);
} catch (Exception e) {
e.printStackTrace();
return -1;
}
}
第一個參數是檔名,第二個參數是你要找的Resource class
getResId("filename", R.drawable.class)
請先 登入 以發表留言。