【问题标题】:display image in imageButton ....在 imageButton 中显示图像 ....
【发布时间】: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


【解决方案1】:

这行得通吗?

imgWeatherIcon = (ImageButton) findViewById(R.id.mndpImage);
imgWeatherIcon.setBackgroundResource(R.drawable.dsnowing);

【讨论】:

  • ImageView类型中的setImageDrawable(Drawable)方法不适用于参数(int)
  • 抱歉,试试 setBackgroundResource(R.drawable.imageName);
  • 更好...但仍然不好...图像出现在用于没有资源的图像视图的“示例图像”后面...如果您理解我的意思...这里是结果shrani.si/f/M/gV/3ny24MGb/problem.png
  • 该图像必须以代码或 xml 的形式分配到某处。找到对图像名称的引用并将其删除。
  • 干得好!它在 XML 文件中......现在我必须弄清楚如何插入变量而不是“dsnowing”......因为我从 XML 文件中调用图像......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-25
  • 1970-01-01
  • 2014-09-05
  • 2014-11-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多