如何为Android中的按钮设置背景图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12034151/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set background image for button in Android?
提问by SampathKumar
I Imported one image inside the drawable-mdpi, then implemented the image from button, but an error occurs no resource found here
. How do I fix this issue?
我在drawable-mdpi里面导入了一张图片,然后从button实现了图片,但是出现了错误no resource found here
。我该如何解决这个问题?
I tried this:
我试过这个:
main.xml
主文件
<Button
android:id="@+id/imageButtonSelector"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable-mdpi/button_focused_orange"/>
回答by Raghav Sood
All drawables are compiled under a single resource name, i.e. drawable
. Android automatically chooses from which folder to take the drawable depending on the screen size, and hence you do not need to specifically point it out. Also, hard coding Android to use resources from a particular folder kind of defeats the purpose of having multiple folders for Android to choose from. To solve this issue, simply change:
所有可绘制对象都在单个资源名称下编译,即drawable
. Android 会根据屏幕大小自动选择从哪个文件夹中获取 drawable,因此您无需特别指出。此外,硬编码 Android 以使用来自特定文件夹类型的资源,这违背了为 Android 提供多个文件夹可供选择的目的。要解决此问题,只需更改:
android:background="@drawable-mdbi/button_focused_orange"/>
To
到
android:background="@drawable/button_focused_orange"/>
回答by Philippe Girolami
Should be @drawable/button_focused_orange
应该 @drawable/button_focused_orange
Not @drawable-mdpi/button_focused_orange
不是 @drawable-mdpi/button_focused_orange
回答by Mark
Try to clean and rebuild your project. If you are usuing Eclipse you can do this by clicking project-> cleanand then project-> Build project
尝试清理并重建您的项目。如果您正在使用 Eclipse,您可以通过单击project-> clean然后单击project-> Build project来执行此操作
回答by Gee
You don't have to mention -mdpi to add background images, simply use drawable only. Here is you revised code. Try this.
您不必提及 -mdpi 来添加背景图像,只需使用 drawable 即可。这是您修改后的代码。尝试这个。
<Button
android:id="@+id/imageButtonSelector"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button_focused_orange"/>