MainActivity如下: 
 
package cn.testreflect; 
import java.lang.reflect.Field; 
import android.os.Bundle; 
import android.widget.ImageView; 
import android.app.Activity; 
/** 
* Demo描述: 
* 依据图片的名字,通过反射获取其在drawable中的ID 
* 在根据此ID显示图片 
*/ 
public class MainActivity extends Activity { 
private ImageView mImageView; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 
init(); 
} 
private void init(){ 
mImageView=(ImageView) findViewById(R.id.imageView); 
getImageByReflect("yaodi"); 
} 
//$表示内部类的意思 
//所以cn.testreflect.R$drawable表示: 
//drawable是cn.testreflect.R的内部类 
private void getImageByReflect(String imageName){ 
try { 
Field field = Class.forName("cn.testreflect.R$drawable").getField(imageName); 
mImageView.setBackgroundResource(field.getInt(field)); 
} catch (Exception e) { 
} 
} 
} 
 main.xml如下: 
 
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
> 
<ImageView 
android:id="@+id/imageView" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_centerInParent="true" 
/> 
</RelativeLayout>