Android Rich Text Processing

Recently I encountered some pit rich text processing, hereby share with you

TextView

Html.fromHtml(data.getPro_job())
The easiest way, the defect can not be resolved ul, lisuch as the type of label.

RichText

RichText.from(data.getPro_job()).into(wvDes);

RichText is a rich text parser in Android platform, supports Html and Markdown, so that you can resolve ulother labels, but the drawback is that there will be problems loading the font style, such as bold, colors and so on.

WebView

Tencent x5 recommended browser to use to see another blog integrated with [FAQ] Tencent X5 Android Web browser

webView.loadDataWithBaseURL("", data.getPro_intro(), "text/html", "utf-8", null);

webviewThere is no problem in the resolution tag, but it raises another question, ScrollViewunder nested at the bottom there will be a large gap.

This problem has two kinds of treatment

  • Gets htmlthe content height, and then set towebview
        wvDes.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView webView, String s) {
                super.onPageFinished(webView, s);
                wvDes.loadUrl("javascript:app.resize(document.body.getBoundingClientRect().height)");
            }
        });

        wvDes.addJavascriptInterface(new WebViewJavaScriptFunction() {
            @Override
            public void onJsFunctionCalled(String tag) {

            }

            @JavascriptInterface
            public void resize(int height) {
                wvDes.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, height + 80);
                        wvDes.setLayoutParams(params);
                    }
                }, 100);

            }

        }, "app");

However, it has been tested on the part of Huawei cell phone, document.body.getBoundingClientRect().heightget the incorrect height, I could do nothing but give up.

  • addView
        mParentLayout.removeAllViews();
        WebView webView = new WebView(this);
        webView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        webView.loadDataWithBaseURL("", data.getPro_intro(), "text/html", "utf-8", null);
        mParentLayout.addView(webView);

Reproduced in: https: //www.jianshu.com/p/f9eeded90f43

Guess you like

Origin blog.csdn.net/weixin_34122810/article/details/91214306