2010-09-14 101 views
6

我一直在尝试使用下面的代码设置一个按钮上的图像,但它似乎并没有工作......我认为我做错了是我正在使用的图像的路径,但我尝试了不同的路径和它不会工作...我已经将我的图像复制到res文件夹中的drawable文件夹中...我在这里做错了什么?在Android的按钮上设置图像?

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

回答

26

为什么不使用

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

您可以使用 “图像按钮” 这是对你更好,,,和这样的代码:

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