int id = mContext.getResources().getIdentifier("drawable/"+drawablename, "drawable",mContext.getPackageName());
(或者)
int id = mContext.getResources().getIdentifier(drawablename, "drawable",mContext.getPackageName());
用于此的 JavaDoc
int android.content.res.Resources.getIdentifier(String name, String defType, String defPackage)
public int getIdentifier (String name, String defType, String defPackage)
Added in API level 1
Return a resource identifier for the given resource name. A fully qualified resource name is of the form "package:type/entry". The first two components (package and type) are optional if defType and defPackage, respectively, are specified here.
Note: use of this function is discouraged. It is much more efficient to retrieve resources by identifier than by name.
Parameters
name The name of the desired resource.
defType Optional default resource type to find, if "type/" is not included in the name. Can be null to require an explicit type.
defPackage Optional default package to find, if "package:" is not included in the name. Can be null to require an explicit package.
Returns
int The associated resource identifier. Returns 0 if no such resource was found. (0 is not a valid resource ID.)