您好我正在尝试以编程方式将图像添加到Android应用的活动
我有这个:
for (int i = 0; i < num_devices; i++) {
ImageView imageView = new ImageView(this);
LinearLayout.LayoutParams vp = new LinearLayout.LayoutParams
(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
imageView.setLayoutParams(vp);
try {
Class res = R.drawable.class;
Field field = res.getField(device_types.get(i));
int resId = field.getInt(null);
imageView = (ImageView) findViewById(resId);
}
catch (Exception e) {
Log.e("MyTag", "Failure to get drawable id.", e);
}
LinearLayout link_devices = (LinearLayout) findViewById(R.id.link_devices);
link_devices.addView(imageView);
然而它并没有让我得到图像的位置(我试着获得0 for getTop,getLeft等..)
我做错了,这是正确的做法
答案 0 :(得分:0)
您没有在该imageview上设置图像,并且已将WRAP_CONTENT设置为布局参数,这意味着imageView的大小与您在其上设置的图像大小相同。 由于没有附加图像,因此imageView大小为0。
尝试使用任何一个代码设置图像; - imageView.setImageBitmap(Bitmap bm),setImageDrawable(Drawable drawable)或setImageResource(int ResID)。
我不知道你正在做什么这个代码&amp;它不是必需的:
Class res = R.drawable.class;
Field field = res.getField(device_types.get(i));
int resId = field.getInt(null);
imageView = (ImageView) findViewById(resId);