我有一个 View ,它有一个方法 setBackgroundImage(BitmapDrawable)
并且我在 res 的 drawable 文件夹中有一个图像 imgbg
。
如何将此图片设置为背景图片?
view.setBackgroundImage(R.drawable.imgbg);
不起作用,因为 R.drawable.imgbg
是一个整数,但方法需要 drawable。
我知道这很愚蠢,但即使经过大量谷歌搜索我也无法解决。
感谢任何帮助。
编辑
此代码 fragment 不属于 Activity 类,因此我没有 context
对象或 getResources()
或 getApplicationContext()
...
编辑 2(解决方案):
public class PieChartDemo01View extends View
{
public PieChartDemo01View(Context context)
{
super(context);
Drawable drawable = super.getResources().getDrawable(
R.drawable.bg2);
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
}
}
最佳答案
view.setBackgroundImage(getResources().getDrawable(R.drawable.imgbg))
关于android - 从资源设置背景图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13744423/