在安卓上如何在按钮上设置图片?

7

我一直在尝试使用以下代码将图片设置为按钮,但似乎无法成功...我认为我做错的是图片的路径,但我尝试了不同的路径仍然无法工作...我已经将我的图片复制到res文件夹中的drawable文件夹中...我在这里做错了什么?

final Button next = (Button) findViewById(R.id.Button02) ;
 Drawable d = Drawable.createFromPath("@drawable/finalarrow1");
  next.setBackgroundDrawable(d);
2个回答

28

为什么不使用

final Button next = (Button) findViewById(R.id.Button02);
next.setBackgroundResource(R.drawable.finalarrow1);

11
你可以使用“图像按钮”,它对你更加友好,而且代码如下:

你可以使用“Image Button”(图像按钮),它对你更加友好,而且代码如下:


final ImageButton next = (ImageButton) findViewById(R.id.Button02) ;
Drawable d = Drawable.createFromPath("@drawable/finalarrow1");
next.enter.setImageDrawable(d);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接