在 Android 上动态添加 imageViews



我想显示来自HTML的图像,我使用Jsoup获取图像url源。但是,一个问题是每个帖子都有不同数量的图片。因此,我无法修复xml布局中图像视图的数量。经过研究,我知道我可以动态创建ImageViews。因此,我创建了ImageViews并将它们插入Linearlayout。但是,我只能看到我最后插入的一张图片。

package kr.ac.mju.hanmaeum.activity.notice;
import android.content.Intent;
import android.media.Image;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import kr.ac.mju.hanmaeum.R;
import kr.ac.mju.hanmaeum.activity.BaseActivity;
import kr.ac.mju.hanmaeum.utils.Constants;

public class NoticeContent extends BaseActivity {
    private String url, title, timestamp, content = "";
    private TextView timestampView, titleView;
    private ImageView contentImageView, contentImageView2;
    private GetImageContentTask getImageContentTask;
    private ArrayList<String> imgList;
    private LinearLayout linearLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notice_content);
        // get intent from MainActivity.
        Intent intent = getIntent();
        url = intent.getStringExtra("URL");
        title = intent.getStringExtra("TITLE");
        timestamp = intent.getStringExtra("TIMESTAMP");
        // Set layout contents and I will change this using ButterKnife and detach them to initFunction.
        timestampView = (TextView) findViewById(R.id.contentTimestamp);
        titleView = (TextView) findViewById(R.id.contentTitle);
        contentImageView = (ImageView) findViewById(R.id.contentImage);
        contentImageView2 = (ImageView) findViewById(R.id.contentImage2);
        linearLayout = (LinearLayout) findViewById(R.id.content_Linear);
        imgList = new ArrayList<String>();
        // get title and timestamp from MainActivity.
        titleView.setText(Constants.NOTICE_TITLE + title);
        timestampView.setText(Constants.NOTICE_TIMESTAMP + timestamp);
        getImageContentTask = new GetImageContentTask();
        getImageContentTask.execute();
    }
    class GetImageContentTask extends  AsyncTask<Void, Void, ArrayList<String>> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }
        @Override
        protected ArrayList<String> doInBackground(Void... params) {
            try {
                Document doc = Jsoup.connect(url).timeout(0).get();
                Elements imgs = doc.select("#divView > img");
                for(Element img : imgs) {
                    imgList.add(img.attr("src"));
                }
            } catch(IOException e) {
                e.printStackTrace();
            }
            return imgList;
        }
        @Override
        protected void onPostExecute(ArrayList<String> imgList) {
            super.onPostExecute(imgList);
            for(int i=0; i< imgList.size(); i++) {
                ImageView imgView = new ImageView(NoticeContent.this);
                Glide.with(NoticeContent.this).load(imgList.get(i)).override(200,200).into(imgView);
                linearLayout.addView(imgView);
            }
        }
    }
}

您需要将 LayoutParams 添加到 ImageView 中,如下所示:

       for(int i=0; i< imgList.size(); i++) {
            ImageView imgView = new ImageView(NoticeContent.this);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(200, 200);
            imgView.setLayoutParams(lp);
            Glide.with(NoticeContent.this)
                 .load(imgList.get(i))
                 .override(200,200)
                 .into(imgView);
            linearLayout.addView(imgView);
        }

试试这个

 for(int i=0; i< imgList.size(); i++) {
        ImageView imgView = new ImageView(this);
        imgView.setId(i++);
        Glide.with(this).load(imgList.get(i)).override(200,200).into((ImageView)findViewById(i++))
        linearLayout.addView(imgView);
    }

for (UserListData userListData : userListDatas){
        Glide.with(getActivity())
                .load(userListData.getAvatar())
                .asBitmap()
                .fitCenter()
                .into(new SimpleTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                        ImageView image = new ImageView(getActivity());
                        image.setImageBitmap(resource);
                        layout.add(image);
                    }
                });
    }

我还没有以这种方式测试过这些,但它应该可以工作。

最新更新