【问题标题】:Unable to set LinearLayout BackgroundResource from URL using Picasso无法使用 Picasso 从 URL 设置 LinearLayout BackgroundResource
【发布时间】:2015-07-17 18:09:07
【问题描述】:

我有一个线性布局,我想以编程方式更改其背景:

<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

附言

我也尝试过使用 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)

【问题讨论】:

    标签: android android-layout background android-linearlayout


    【解决方案1】:

    Picasso 仅适用于 ImageView,不适用于布局。

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

    【讨论】:

      【解决方案2】:

      您必须在后台线程中而不是在主线程中这样做。考虑使用图片加载库如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;
              }
          }
      

      【讨论】:

      • 我尝试了您建议的方法 - 请查看我上面更新的源代码。
      • setBackgroundDrawable() 已弃用。
      • @peresisUser 轻松修复队友,只需将其更改为 setBackground() 它将调用 setBackgroundDrawable()
      猜你喜欢
      • 2019-08-20
      • 1970-01-01
      • 2019-04-03
      • 1970-01-01
      • 2017-07-28
      • 2017-02-02
      • 2020-04-22
      • 2019-09-15
      • 2018-12-24
      相关资源
      最近更新 更多