2013-09-23 185 views
4
gridcell = (Button) row.findViewById(R.id.calendar_day_gridcell);  
gridcell.setText("Day 1");  
URL url = new URL("http://172.16.4.29:81/pht/2013/9/18/3027_2013_9_18_12_14_56_b.JPG"); 
Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream()); 

如何設置bitmap將bmp圖片設置爲按鈕gridcells背景圖片?如何將位圖圖像設置爲按鈕背景圖片

+0

是不是你有幫助的答案?如果你找到解決方案,請把你的.. –

+0

看看[Android ImageButton示例](http://www.mkyong.com/android/android-imagebutton-example/)。 – kenorb

回答

12

您可以使用下面的代碼來做到這一點..

BitmapDrawable bdrawable = new BitmapDrawable(context.getResources(),bitmap); 

只需要設置位圖如下功能

button.setBackground(bdrawable); 
+1

'BitmapDrawable(bitmap)'已棄用。 – offset

+0

@offset感謝您通知。答案已編輯:) –

+0

ImageButton#setBackgroundDrawable(Drawable)也被棄用。 Sruit A.Suk的回答比較好。 – n8yn8

2

你需要檢查當前版本的Android也

Button bProfile; // your Button 
Bitmap bitmap; // your bitmap 

if(android.os.Build.VERSION.SDK_INT < 16) { 
    bProfile.setBackgroundDrawable(new BitmapDrawable(getResources(), bitmap)); 
} 
else { 
    bProfile.setBackground(new BitmapDrawable(getResources(),bitmap)); 
}