Android 如何制作compoundView

Android 如何制作compoundView,android,html,textview,Android,Html,Textview,我有一个场景,其中有一个字符串,其中有HTML标记。在html标记中,可以有多个或一个img标记。现在我必须显示嵌入文本中的图像。为此,我使用了textview,并遵循这一伟大的原则。但它并没有给我要求的结果。于是,我在网上搜索,发现了CompoundView 注意仅限于使用webview 我已经研究了这些教程 Ryanharter的博客 但从哪里开始,如何开始,我并没有得到太多。因为我不知道字符串中会有多少图像标签 如果你们中有人能指导我怎么做就太好了。一些指导方针将不胜感激 提前谢谢 经

我有一个场景,其中有一个
字符串
,其中有
HTML
标记。在
html
标记中,可以有多个或一个
img
标记。现在我必须显示嵌入文本中的图像。为此,我使用了textview,并遵循这一伟大的原则。但它并没有给我要求的结果。于是,我在网上搜索,发现了
CompoundView

注意仅限于使用webview

我已经研究了这些教程

  • Ryanharter的博客
  • 但从哪里开始,如何开始,我并没有得到太多。因为我不知道字符串中会有多少图像标签

    如果你们中有人能指导我怎么做就太好了。一些指导方针将不胜感激


    提前谢谢

    经过几个小时的挖掘,我找到了一些解决方案,下面是代码和一些细节

    这个答案可能对某人有所帮助。我使用Jsoup提取
    ();
    List li=new ArrayList();
    文档doc=Jsoup.parse(内容);
    Elements=doc.getAllegements();
    for(元素:元素){
    Tag Tag=element.Tag();
    if(tag.getName().matches(“h[1-6]{1}”)){
    String heading=element.select(tag.getName().toString()).text();
    TextView TextView=新的TextView(上下文);
    textView.setLayoutParams(新的LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_父级,
    LinearLayout.LayoutParams.WRAP_内容);
    LinearLayout.LayoutParams params=(LinearLayout.LayoutParams)textView.getLayoutParams();
    int top=(int)context.getResources().getDimension(R.dimen.heading\u margin\u top);
    int start=(int)context.getResources().getDimension(R.dimen.content\u margin);
    int end=(int)context.getResources().getDimension(R.dimen.content\u margin);
    参数设置边距(开始、顶部、结束、0);
    textView.setTextSize(20);
    textView.setTypeface(BiterboldFont);
    textView.setText(标题);
    setTextColor(context.getResources().getColor(R.color.black));
    linearLayout.addView(文本视图);
    }
    if(tag.getName().equalsIgnoreCase(“p”)){
    元素。选择(“img”).remove();
    字符串体=element.html();
    TextView TextView=新的TextView(上下文);
    textView.setLayoutParams(新的LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_父级,
    LinearLayout.LayoutParams.WRAP_内容);
    LinearLayout.LayoutParams params=(LinearLayout.LayoutParams)textView.getLayoutParams();
    int top=(int)context.getResources().getDimension(R.dimen.heading\u margin\u top);
    int start=(int)context.getResources().getDimension(R.dimen.content\u margin);
    int end=(int)context.getResources().getDimension(R.dimen.content\u margin);
    参数设置边距(开始、顶部、结束、0);
    textView.setTypeface(BiterboldFont);
    textView.setLinksClickable(true);
    textView.setAutoLinkMask(Linkify.WEB_URL);
    setText(Html.fromHtml(body));
    textView.setTextColor(context.getResources().getColor(R.color.content_color));
    linearLayout.addView(文本视图);
    p、 添加(正文);
    }
    if(tag.getName().equalsIgnoreCase(“ol”)){
    字符串ol=element.select(tag.getName().toString()).outerHtml();
    TextView TextView=新的TextView(上下文);
    textView.setLayoutParams(新的LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_父级,
    LinearLayout.LayoutParams.WRAP_内容);
    LinearLayout.LayoutParams params=(LinearLayout.LayoutParams)textView.getLayoutParams();
    参数setMarginStart((int)context.getResources().getDimension(R.dimen.content_margin));
    int top=(int)context.getResources().getDimension(R.dimen.heading\u margin\u top);
    int start=(int)context.getResources().getDimension(R.dimen.content\u margin);
    int end=(int)context.getResources().getDimension(R.dimen.content\u margin);
    参数设置边距(开始、顶部、结束、0);
    textView.setTypeface(BiterboldFont);
    textView.setLinksClickable(true);
    textView.setAutoLinkMask(Linkify.WEB_URL);
    textView.setTextColor(context.getResources().getColor(R.color.content_color));
    textView.setText(Html.fromHtml(ol,null,new MyTagHandler());
    linearLayout.addView(文本视图);
    }
    if(tag.getName().equalsIgnoreCase(“ul”)){
    字符串ul=element.select(tag.getName().toString()).outerHtml();
    TextView TextView=新的TextView(上下文);
    textView.setLayoutParams(新的LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_父级,
    LinearLayout.LayoutParams.WRAP_内容);
    LinearLayout.LayoutParams params=(LinearLayout.LayoutParams)textView.getLayoutParams();
    int top=(int)context.getResources().getDimension(R.dimen.heading\u margin\u top);
    int start=(int)context.getResources().getDimension(R.dimen.content\u margin);
    int end=(int)context.getResources().getDimension(R.dimen.content\u margin);
    参数设置边距(开始、顶部、结束、0);
    textView.setTypeface(BiterboldFont);
    textView.setLinksClickable(true);
    textView.setAutoLinkMask(Linkify.WEB_URL);
    textView.setTextColor(context.getResources().getColor(R.color.content_color));
    textView.setText(Html.fromHtml(ul,null,newMyTagHandler());
    linearLayout.addView(文本视图);
    }
    if(tag.getName().equalsIgnoreCase(“img”)){
    字符串url=element.select(“img”).attr(“src”);
    最终图像视图=新图像视图(上下文);
    imageView.setL
    
    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;
    }
    }