【发布时间】:2013-08-12 12:14:42
【问题描述】:
我在 imageButton 上显示图像时遇到问题...
imgWeatherIcon = (ImageButton) findViewById(R.id.mndpImage);
Intent i = getIntent();
this.iconfile = i.getStringExtra("icon");
String uri = "drawable/"+ "d" + iconfile;
System.out.println("* "+ uri);
int imageBtnResource = getResources().getIdentifier(uri, null, getPackageName());
Drawable dimgbutton = getResources().getDrawable(imageBtnResource);
imgWeatherIcon.setImageDrawable(dimgbutton);
一些 System.out.println 输出:
uri = drawable/dsnowing
imageBtnResource = 2130837512
dimgbutton = android.graphics.drawable.BitmapDrawable@414733c8
图片存在于drawable文件夹中 布局中 imageButton 元素 (mndpImage) 的名称是正确的
知道如何解决这个问题吗?
【问题讨论】:
-
我很困惑为什么你使用可绘制文件夹的路径而不是 R.drawable.imageName
-
我是 android 编程新手...我找到了这段代码,现在我正在尝试对其进行测试...所以如果您知道正确的方法...请:)
-
如果您在可绘制文件夹中有项目,而不是使用图像的路径,您可以使用 R.drawable.imageName 所以对于名为 dsnowing 的图像,它将是 R.drawable.dsnowing 如果你在 ldpi/mdpi 等中有多种尺寸/密度的图像。它也会根据设备硬件自动选择正确的图像。
-
imgWeatherIcon.setImageDrawable(R.drawable.dsnowing);
标签: imageview drawable imagebutton