2013-05-29 42 views
0

我想以編程方式將圖像設置爲底部按鈕。我在java代碼中創建了show more按鈕,並且想要在按鈕的底部分配箭頭圖像,但是沒有找到用於指定圖像的方法。
這是我的按鈕代碼。Android:以編程方式指定按鈕底部的圖像

btnAddHotel = new Button(this); 
    btnAddHotel.setText("show more"); 
    btnAddHotel.setBackgroundColor(Color.TRANSPARENT); 
    btnAddHotel.setTextColor(Color.WHITE); 
    btnAddHotel.setTypeface(faceNormal); 
    btnAddHotel.setTextSize(12); 

請給我任何參考方法,在按鈕的底部分配圖像。
在此先感謝。

+0

你的意思是相當於Java的XML屬性'的android:drawableBottom = 「@繪製/ ......」'? –

+0

是的....我想java代碼相當於xml屬性... –

回答

1

您可以使用setCompoundDrawablesWithIntrinsicBounds()來做到這一點。構造函數是:

setCompoundDrawablesWithIntrinsicBounds (int left, int top, int right, int bottom) 

而且你不希望指定一個圖像資源,你只是把在自己的位置的位置。例如:

btnAddHotel.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, R.drawable.some_image_resource); 

瞭解更多關於setCompoundDrawablesWithIntrinsicBounds here

編輯:由於Android的Button繼承了TextView Widget屬性,指着TextView文檔上面的鏈接仍然是有效的。

0

,如果我理解你的問題正確,你可以在你的按鈕使用setBackgroundDrawable()

btnAddHotel.setBackgroundDrawable(R.drawable.xxx); 
相關問題