我的情况是String
中有HTML
个标记。在html
标记中,可以有多个或一个img
标记。现在我必须显示嵌入在文本中的图像。为此,我使用的是textview,我遵循了这个伟大的answer。但它没有给我所需的结果。所以,然后我搜索了网络和遇到了CompoundView
注意限制使用webview。
我已经研究了这些教程。
答案 0 :(得分:-1)
因此,经过几个小时的挖掘后,我发现这里有一些解决方案,其中包含一些细节。
这个答案可能有助于某人。我使用Jsoup从字符串中提取<Img/>
标记,然后在ImageView
和<p>
Textview
中显示图像。结果是根据我的需要。我也使用Universal Image Loader Libaray在ImageView
中加载图像然后我以编程方式将视图添加到我的案例布局中的布局是线性布局,所以我做了一个帮助类并传递内容,html字符串和线性布局作为参数。
在项目中添加jsoup。
compile 'org.jsoup:jsoup:1.9.2'
这里有一些片段。
public class PostContentHandler {
Context context;
String content;
LinearLayout linearLayout;
public PostContentHandler(Context context, String content, LinearLayout linearLayout) {
this.context = context;
this.content = content;
this.linearLayout = linearLayout;
}
public void setContentToView() {
//custom font
Typeface bitterBoldFont = Typeface.createFromAsset(context.getAssets(), "fonts/Bitter-Regular.otf");
List<String> p = new ArrayList<>();
List<String> src = new ArrayList<>();
List<String> li = new ArrayList<>();
Document doc = Jsoup.parse(content);
Elements elements = doc.getAllElements();
for (Element element : elements) {
Tag tag = element.tag();
if (tag.getName().matches("h[1-6]{1}")) {
String heading = element.select(tag.getName().toString()).text();
TextView textView = new TextView(context);
textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) textView.getLayoutParams();
int top = (int) context.getResources().getDimension(R.dimen.heading_margin_top);
int start = (int) context.getResources().getDimension(R.dimen.content_margin);
int end = (int) context.getResources().getDimension(R.dimen.content_margin);
params.setMargins(start, top, end, 0);
textView.setTextSize(20);
textView.setTypeface(bitterBoldFont);
textView.setText(heading);
textView.setTextColor(context.getResources().getColor(R.color.black));
linearLayout.addView(textView);
}
if (tag.getName().equalsIgnoreCase("p")) {
element.select("img").remove();
String body = element.html();
TextView textView = new TextView(context);
textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) textView.getLayoutParams();
int top = (int) context.getResources().getDimension(R.dimen.heading_margin_top);
int start = (int) context.getResources().getDimension(R.dimen.content_margin);
int end = (int) context.getResources().getDimension(R.dimen.content_margin);
params.setMargins(start, top, end, 0);
textView.setTypeface(bitterBoldFont);
textView.setLinksClickable(true);
textView.setAutoLinkMask(Linkify.WEB_URLS);
textView.setText(Html.fromHtml(body));
textView.setTextColor(context.getResources().getColor(R.color.content_color));
linearLayout.addView(textView);
p.add(body);
}
if (tag.getName().equalsIgnoreCase("ol")) {
String ol = element.select(tag.getName().toString()).outerHtml();
TextView textView = new TextView(context);
textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) textView.getLayoutParams();
params.setMarginStart((int) context.getResources().getDimension(R.dimen.content_margin));
int top = (int) context.getResources().getDimension(R.dimen.heading_margin_top);
int start = (int) context.getResources().getDimension(R.dimen.content_margin);
int end = (int) context.getResources().getDimension(R.dimen.content_margin);
params.setMargins(start, top, end, 0);
textView.setTypeface(bitterBoldFont);
textView.setLinksClickable(true);
textView.setAutoLinkMask(Linkify.WEB_URLS);
textView.setTextColor(context.getResources().getColor(R.color.content_color));
textView.setText(Html.fromHtml(ol, null, new MyTagHandler()));
linearLayout.addView(textView);
}
if (tag.getName().equalsIgnoreCase("ul")) {
String ul = element.select(tag.getName().toString()).outerHtml();
TextView textView = new TextView(context);
textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) textView.getLayoutParams();
int top = (int) context.getResources().getDimension(R.dimen.heading_margin_top);
int start = (int) context.getResources().getDimension(R.dimen.content_margin);
int end = (int) context.getResources().getDimension(R.dimen.content_margin);
params.setMargins(start, top, end, 0);
textView.setTypeface(bitterBoldFont);
textView.setLinksClickable(true);
textView.setAutoLinkMask(Linkify.WEB_URLS);
textView.setTextColor(context.getResources().getColor(R.color.content_color));
textView.setText(Html.fromHtml(ul, null, new MyTagHandler()));
linearLayout.addView(textView);
}
if (tag.getName().equalsIgnoreCase("img")) {
String url = element.select("img").attr("src");
final ImageView imageView = new ImageView(context);
imageView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
final ProgressBar progressBar = new ProgressBar(context);
linearLayout.addView(progressBar);
progressBar.setVisibility(View.GONE);
ImageLoader imageLoader = ImageLoader.getInstance();
imageLoader.displayImage(url, imageView, new SimpleImageLoadingListener() {
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
super.onLoadingComplete(imageUri, view, loadedImage);
progressBar.setVisibility(View.INVISIBLE);
int height = loadedImage.getHeight();
imageView.getLayoutParams().height = getScreenWidth();
imageView.setAdjustViewBounds(true);
imageView.requestLayout();
}
@Override
public void onLoadingStarted(String imageUri, View view) {
super.onLoadingStarted(imageUri, view);
progressBar.setVisibility(View.VISIBLE);
}
});
linearLayout.addView(imageView);
src.add(url);
}
}
}
public static int getScreenWidth() {
return Resources.getSystem().getDisplayMetrics().widthPixels;
}
}
我希望我的回答会对某人有所帮助。