2014-12-08 65 views
0

我的代码是这样广东话设置线性布局背景从路径

ll.setBackground(Drawable.createFromPath(new ImageLoader().fullPath+"/desiredFilename.png"));

LL是我的线性布局的对象,这是正确的方法

Bitmap bitmap = BitmapFactory.decodeFile(new ImageLoader().fullPath+"/desiredFilename.png"); 
 
\t \t    Resources res=getResources(); 
 
\t \t    BitmapDrawable bitmapDrawable = new BitmapDrawable(res,bitmap); 
 
\t \t    ll.setBackground(bitmapDrawable);

我也用这个代码,但dosent工作 显示错误noSuchMethods

+0

[http://stackoverflow.com/questions/12781501/android-设置linearlayout背景编程](http://stackoverflow.com/questions/12781501/android-setting-linearlayout-background-programmatically) – 2014-12-08 07:00:43

回答

2

尝试这种方式

ll.setBackgroundDrawable(bitmapDrawable); 

,而不是

ll.setBackground(bitmapDrawable); 
+0

感谢您的帮助,我已经尝试过,其作品完美,那么为什么我问这个问题意味着ll。 setBackgroundDrawable(bitmapDrawable);似乎被弃用,所以即时寻找一个新的解决方案 – 2014-12-08 06:58:25

+0

@AmeenMaheen检查这个[http://stackoverflow.com/questions/12523005/android-set-background-drawable-programmatically](http://stackoverflow.com/questions/ 12523005/android-set-background-drawable-programmatically) – 2014-12-08 06:59:53

+0

:-)如果我不得不像我的问题中的代码一样从SD卡加载图像,我该怎么办? – 2014-12-08 07:03:43