无法使用Picasso从URL设置LinearLayout BackgroundResource



我有一个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(渣油);

然而,背景图像永远不会加载,没有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

公立小学

我也尝试过使用Picasso完成同样的工作-我不确定如何绕过所述的错误并成功加载:

源:

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)

Picasso只能使用ImageViews,不能使用layouts。

你可以在你的布局中放置一个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);

你必须在后台线程而不是主线程中这样做。考虑使用图像加载库,如Picasso

是例子

确保你在LinearLayout中有一个图像视图,并让它填充_parent(即LinearLayout), Picasso不直接适用于ImageView,因此会出现错误。

ImageView imageView = (ImageView) findViewById(R.id.imageView); 
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);

或者

LinearLayout linearLayout=(LinearLayout)findViewById(R.id.yourLinearLayoutid);
BitmapDrawable drawableBitmap=new BitmapDrawable(getBitmap(urlString));
linearLayout.setBackgroundDrawable(drawableBitmap);

这个方法getBitMap,就像Piccasso库中的方法一样,不需要库就足够了。

private Bitmap getBitmap(String url)
    {

        //from web
        try {
            Bitmap bitmap=null;
            URL imageUrl = new URL(url);
            HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection();
            conn.setConnectTimeout(30000);
            conn.setReadTimeout(30000);
            conn.setInstanceFollowRedirects(true);
            InputStream is=conn.getInputStream();
            OutputStream os = new FileOutputStream(f);
            Utils.CopyStream(is, os);
            os.close();
            bitmap = decodeFile(f);
            return bitmap;
        } catch (Exception ex){
           ex.printStackTrace();
           return null;
        }
    }

最新更新