2012-10-27 184 views
2

我只是一个简单的问题。如何在按钮上设置位图?我使用的方法如何在按钮上设置位图?

.setBackgroundResource(int resid) 

当我有可绘制的资源。但是,我可以为位图做些什么?

回答

7

使用ImageButton,并调用setImageBitmap()来设置前景图像。 setBackgroundResource()用于按钮背景,需要使用StateListDrawable才能生效。

+0

Button没有什么可做的吗? – GVillani82

+2

@ Joseph82:'Button'用于文字说明。 'ImageButton'用于图像。你可以使用带'Button'的'setCompoundDrawablesWithIntrinsicBounds()'来放置一个图像和一个标题,尽管你必须为这些对象创建'BitmapDrawable'对象。 – CommonsWare

1

拨打电话setImageBitmap在您的按钮上。

myButton.setImageBitmap(myBitmap); 
+2

我可以找到setImageBitmap方法。你确定是一个Button类的方法吗? – GVillani82

+0

它来自ImageView。我在猜测。 – wtsang02

+0

您需要使用'ImageButton',它是'ImageView'的子类http://developer.android.com/reference/android/widget/ImageButton.html – Doomsknight