2010-09-14 94 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);