【问题标题】:How to set background image for button in Android?如何在Android中为按钮设置背景图像?
【发布时间】:2012-08-20 08:05:40
【问题描述】:

我在 drawable-mdpi 中导入了一张图像,然后从按钮实现了图像,但出现错误no resource found here。我该如何解决这个问题?

我试过了:

main.xml

  <Button
        android:id="@+id/imageButtonSelector"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable-mdpi/button_focused_orange"/>

【问题讨论】:

  • 由于您是新来的,您可以通过单击“勾选”小箭头来接受答案。这将提高您的接受率,甚至帮助面临类似问题的其他人

标签: android button background drawable


【解决方案1】:

所有可绘制对象都在一个资源名称下编译,即drawable。 Android 会根据屏幕大小自动选择从哪个文件夹获取可绘制对象,因此您无需特别指出。此外,硬编码 Android 以使用来自特定文件夹类型的资源违背了让 Android 选择多个文件夹的目的。要解决此问题,只需更改:

    android:background="@drawable-mdbi/button_focused_orange"/>

    android:background="@drawable/button_focused_orange"/>

【讨论】:

  • 源文件中出现错误,R 无法解析为变量。如何解决此错误?请帮帮我
  • 现在是进入 Project -> Clean 的好时机。另外,确保没有 import android.R; Java 文件中的行类型。
  • 感谢您的回复,现在我清理项目并删除android.R,然后我运行应用程序,再次出现同样的错误
  • 运行时不会出现构建错误。是不可能的。检查 LogCat 以查看您遇到的错误。
  • 当我在可绘制文件夹中导入图像时,出现该错误
【解决方案2】:

应该是@drawable/button_focused_orange

不是@drawable-mdpi/button_focused_orange

【讨论】:

  • 我有一个疑问,请您帮忙
  • 源文件出错,R不能解析为变量
【解决方案3】:

尝试清理并重建您的项目。 如果您使用的是 Eclipse,您可以通过单击 project -> clean 然后 project -> Build project 来执行此操作

【讨论】:

  • 他的 XML 有问题。清理项目而不解决问题不会改变任何事情。
  • 而且...有时清理会修复 Android 构建过程中的缓存构建。当你被难住时,这真的是一件事情。
  • @RaghavSood 你说得对,我没抓到那个。由于 bitbox 所说的,我留下了答案
【解决方案4】:

您不必提及 -mdpi 来添加背景图像,只需使用 drawable 即可。这是您修改后的代码。试试这个。

<Button
    android:id="@+id/imageButtonSelector"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/button_focused_orange"/>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-18
    相关资源
    最近更新 更多