我试着把图标设置为myButton,它能工作!(见下面的代码)。
Button myButton = new Button(this);
myButton.setCompoundDrawablesWithIntrinsicBounds(R.drawable.abc_ic_menu_cut_mtrl_alpha, 0, 0, 0);
现在,我从一个URL,位图下载了一张图片。
我的问题是如何通过将R.drawable.abc_ic_menu_cut_mtrl_alpha
替换为bitmap
来设置myButton的图标。
谢谢你的帮助。
发布于 2015-07-26 05:21:08
是工作!
BitmapDrawable bd = new BitmapDrawable(getResources(), mybitmap);
myButton.setCompoundDrawablesWithIntrinsicBounds(bd,null,null,null);
谢谢你,Rishad Appat,rekire和大家我能做到。
发布于 2015-07-26 03:55:42
首先,必须从位图中创建可绘制的位图。
BitmapDrawable bdrawable = new BitmapDrawable(context.getResources(),bitmap);
然后设置这样的按钮..。
Button btn = (Button) fndViewById(R.id.button);
btn.setBackgroundDrawable(bdrawable);
发布于 2015-07-26 03:55:22
您可以使用TextView.setCompoundDrawables(...)
方法,这需要一个Drawable
,因此需要用BitmapDrawable
包装位图。
这应该是可行的:
BitmapDrawable drawable = new BitmapDrawable(getResources(), yourBitmap);
yourButton.setCompoundDrawables(drawable, null, null, null);
如果没有正确显示这一点,那么您需要使用以下调用来修正自己的界限:
drawable.setBounds(0, 0, yourBitmap.getWidth(), yourBitmap.getHeight());
https://stackoverflow.com/questions/31636603
复制相似问题