在android中的按钮上设置图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3709009/
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
set image on a button in android?
提问by hemant
i have been trying to set a image on a button using the following code but it doesn't seem to work...i think what i am doing wrong is the path of the image i am using but i tried different paths and it wont work...i have copied my image to the drawable folder in res folder...what am i doing wrong here??
我一直在尝试使用以下代码在按钮上设置图像,但它似乎不起作用......我认为我做错的是我正在使用的图像的路径,但我尝试了不同的路径,但它不会工作...我已将我的图像复制到 res 文件夹中的 drawable 文件夹...我在这里做错了什么?
final Button next = (Button) findViewById(R.id.Button02) ;
Drawable d = Drawable.createFromPath("@drawable/finalarrow1");
next.setBackgroundDrawable(d);
回答by Kan-Ru Chen
Why not use
为什么不使用
final Button next = (Button) findViewById(R.id.Button02);
next.setBackgroundResource(R.drawable.finalarrow1);
回答by Oubaida AlQuraan
You can use the "Image Button" it's better for you ,,, and the code like this :
您可以使用“图像按钮”,它对您更好,和这样的代码:
final ImageButton next = (ImageButton) findViewById(R.id.Button02) ;
Drawable d = Drawable.createFromPath("@drawable/finalarrow1");
next.enter.setImageDrawable(d);