我有一个linearLayout我想以编程方式更改背景:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/downloadLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1" >
...
并且我尝试使用以下方法设置XML布局的背景图片:
LinearLayout linearLayout2 = (LinearLayout) findViewById(R.id.downloadLayout);
int resId = getResources().getIdentifier(background,
"drawable", getPackageName());
linearLayout2.setBackgroundResource(resId);
但是,背景图像永远不会加载,没有NPE,图像根本就不会加载。任何建议表示赞赏。
我已经进行了一些调试,并且当前具有以下值:
linearLayout2 = android.widget.LinearLayout{529b3f58 V.E..... ......I. 0,0-0,0 #7f0a008e app:id/downloadLayout}
background = http://xxx.xxx.x.xxx/bgs/big_lebowski_bg.jpg
resID = 0
附言
我还尝试过使用毕加索完成相同的操作-我不确定如何解决所述错误并成功加载它:
资源:
final LinearLayout downloadLayout = (LinearLayout) findViewById(R.id.downloadLayout);
Picasso.with(this).load("http://i.imgur.com/DvpvklR.png").into(downloadLayout);
错误:
The method into(Target) in the type RequestCreator is not applicable for the arguments (LinearLayout)
最佳答案
毕加索仅适用于ImageViews,不适用于布局。
您可以将ImageView放入布局中,将其设置为与父级的宽度和高度匹配,然后将图像注入ImageView中。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/downloadLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1" >
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
然后,这应该起作用:
ImageView imageView = (ImageView) findViewById(R.id.imageView);
Picasso.with(this).load("http://i.imgur.com/DvpvklR.png").into(imageView);