搜尋此網誌

2012年11月13日 星期二

Android 得到identifie resource id

Method 1: Using Java reflection features to get the resource ID.
public static int getResourceId(String name){
        int drawableId = -1 ;
        try {
            Class<drawable> res = R.drawable.class;
            Field field = res.getField(name);
            drawableId = field.getInt(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return drawableId;
    }
Method 2: Using Android Resources API.
public int getIdentifier (String name, String defType, String defPackage)