如何将位图图像设置为按钮背景图像

Vir*_*esh 4 grid android cell bmp

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());
Run Code Online (Sandbox Code Playgroud)

如何将bitmapbmp图像设置为按钮gridcells背景图像?

Chi*_*hod 13

你可以使用以下代码来做到这一点..

BitmapDrawable bdrawable = new BitmapDrawable(context.getResources(),bitmap);
Run Code Online (Sandbox Code Playgroud)

然后用下面的函数设置位图

button.setBackground(bdrawable);
Run Code Online (Sandbox Code Playgroud)


Sha*_*aun 0

我始终强烈推荐您使用图像按钮。

http://developer.android.com/reference/android/widget/ImageButton.html

我希望这对你有帮助,肖恩。