android webview 全屏100%顯示圖片

這裏引用 第三方類庫 html

implementation 'org.jsoup:jsoup:1.10.2'

定義工具類 HtmlUtils
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

 
public class HtmlUtils {
    /**
     * 將html文本內容中包含img標籤的圖片,寬度變爲屏幕寬度,高度根據寬度比例自適應
     **/
    public static String getNewContent(String htmltext){
        try {
            Document doc= Jsoup.parse(htmltext);
            Elements elements=doc.getElementsByTag("img");
            for (Element element : elements) {
                element.attr("width","100%").attr("height","auto");
            }

            return doc.toString();
        } catch (Exception e) {
            return htmltext;
        }
    }
}
View Code

使用方法node

 if(!TextUtils.isEmpty(mCourseDetailRes.getVideo_intro())) {
                   mWebView.setVisibility(View.VISIBLE);
                   WebSettings webSettings = mWebView.getSettings();
                   // 啓用JS
                   webSettings.setJavaScriptEnabled(true);
                   webSettings.setUseWideViewPort(true);
                   webSettings.setLoadWithOverviewMode(true);
                   webSettings.setBuiltInZoomControls(false);//開啓zoom
                   webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
                   webSettings.setDisplayZoomControls(false);
                   mWebView.setWebViewClient(new WebViewClient());
                   String html=HtmlUtils.getNewContent(mCourseDetailRes.getVideo_intro());
                   mWebView.loadData(html+ "", "text/html", "UTF-8");
               }
View Code

 注意 webview loadData 顯示中文亂碼,加上charset=UTF-8web


mWebView.loadData(html + "", "text/html; charset=UTF-8", "UTF-8");
字體放大功能
webSettings.setTextZoom(220);//字體大小
相關文章
相關標籤/搜索