Android: Replace lt;imggt;-tag with ImageViews(Android:用 ImageViews 替换 lt;imggt;-tag)
问题描述
我正在使用 Html.fromHtml 在 TextView 中显示一些 HTML,效果很好.
I'm showing a bit of HTML in a TextView using Html.fromHtml, which works great.
有时这个 html 会有 img-tags,我也想显示这些.
Sometimes this html will have img-tags, and I'd also like to display these.
我试过 Android HTML ImageGetter as AsyncTask 但这似乎很慢且不可预测(大小图片).
I tried Android HTML ImageGetter as AsyncTask but this seemed slow and unpredictable (size of the image).
示例 HTML(可能不同...):
Example HTML (it can differ...):
<h2>title</h2><br /><p>TEXT</p>
<p style="text-align: center;">Anyways, fokea?.<img src="http://xxxximages/1048268-11-1426170876314.jpg" alt="" /><br /><br /><br /></p>
<p>Jo, veldig mye blomster og duftelys. elt feil sk. Luksus!</p>
<p style="text-align: center;"><img src="http://xxxx/images/1048268-11-1426171000272.jpg" alt="" /></p>
<p><strong>Håper dere har det hakket bedre enn meg.</strong> </p>
我考虑过使用 JSOUP 来获取所有 img-tags,然后创建 X 数量的 ImageView(并用 Picasso 填充它们).这工作正常,但图像总是在文本的顶部或底部.
I thought about using JSOUP to get all img-tags, and then create X amounts of ImageViews (and populating them with Picasso). This works OK, but the images are always either on top or the bottom of text.
替换每个 img-tag 的最佳解决方案是什么,并根据文本在正确的位置为每个 img-tag 创建一个新的 ImageView?
What would be the best solution for replacing every img-tag and for each one, at the correct place according to the text, create a new ImageView?
好吧,既然没有人有任何建议,我就做了这个又快又脏的.
Well, since no-one had any suggestions, I made this quick and dirty one.
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);
}
}
}
虽然我确信代码远非最佳,但它看起来和工作起来都很棒.
It looks and works fantastic, although I'm sure the code is far from optimal.
推荐答案
我用这个解决了:
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);
}
}
}
这篇关于Android:用 ImageViews 替换 <img>-tag的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Android:用 ImageViews 替换 <img>-tag
基础教程推荐
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01