如何制作复合视图 [英] How to make compoundView
问题描述
我有一个场景,其中包含 HTML
标记的 String
。在 html
标记中,可以有多个或一个 img
标记。现在,我必须显示嵌入到文本中的图像。为此,我使用了textview,并且遵循了这个伟大的答案。但它没有给我需要的结果。所以,我在网上搜索了一下。遇到 CompoundView
I have a scenario where I have a String
which has HTML
tag in it. In html
tag,there can be many or one img
tag.Now I have to show images embedded in text. For that I was using textview and I followed this great answer. But it isn't giving me required result. So, then I searched on web & came across CompoundView
注意限制要使用webview。
Note Restricted to want to use webview.
我已经看过这些教程。
- tutplus
- vogella
- Ryanharter's blog post
但是我从哪里开始以及如何开始并没有太多收获。因为我不知道字符串中会有多少图片标签。
But I am not getting much from where to start and how to start with. Since I have no idea how many image tag would be there in string.
如果你们中的任何人指导我如何去做,那将是非常好的。一些准则将不胜感激。
It would be very nice if anyone of you guide me how to do it. Some guidelines would be much appreciated.
预先感谢!
推荐答案
所以经过几个小时的挖掘,我发现一些解决方案是一些细节的代码。
So after some hours of digging i found some solution here is the code with some details.
这个答案可能有助于某人。我使用Jsoup从字符串中提取< Img />
标记,然后在 ImageView
中显示图像,然后< p>
在 Textview
中。结果是根据我需要的。我也使用通用图像加载器Libaray加载图像 ImageView
然后我以编程方式添加视图到布局在我的情况布局是linearlayout,所以我做了一个辅助类和传递的内容, HTML字符串和线性布局作为参数。
This answer may help someone. I used Jsoup to extract <Img/>
tag out of the string then i show the image in ImageView
and <p>
in Textview
. Results was according to what i needed. Also i used Universal Image Loader Libaray to load images in ImageView
Then i added view programmatically to the layout in my case layout was the linearlayout so i made a helper class and passed content,html string and linear layout as the parameter.
在您的项目中添加jsoup。
add jsoup in your project.
compile 'org.jsoup:jsoup:1.9.2'
这里有一些片段。 / p>
Here some snippet.
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;
}
}
我希望我的回答能帮助别人。
I hope my answer will help someone.
这篇关于如何制作复合视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!