Android: Replace -

后端 未结 1 1950
情深已故
情深已故 2021-01-14 19:55

I\'m showing a bit of HTML in a TextView using Html.fromHtml, which works great.

Sometimes this html will have img-tags, and I\'d also like to display these.

相关标签:
1条回答
  • 2021-01-14 20:25

    I solved it using this:

    ArrayList<String> lines = new ArrayList<>();
    Scanner scanner = new Scanner(content);
    while (scanner.hasNextLine()) {
        String line = scanner.nextLine();
        lines.add(line);
    }
    scanner.close();
    
    for(int i = 0; i < lines.size(); i++) {
        Document doc = Jsoup.parse(lines.get(i));
        Elements imgs = doc.select("img");
        if(imgs.size() == 0) {
            TextView textView = new TextView(this);
            textView.setText(Html.fromHtml(lines.get(i)));
            maincontainer.addView(textView);
        } else {
            for(Element el : imgs) {
                Element img = el.select("img").first();
                String image = img.absUrl("src");
                ImageView imageView = new ImageView(this);
                imageView.setPadding(0, 10, 0, 10);
                imageView.setAdjustViewBounds(true);
                Picasso.with(getApplicationContext()).load(image).into(imageView);
                maincontainer.addView(imageView);
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题