Android webview加載富文本 屏幕簡單適配

UI界面

這個沒有什麼,寫個webview控件就ok

<WebView
    android:id="@+id/web_introduction"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

Java代碼

getHtmlData() 爲富文本全屏適配的方法

String tv_html = coursePageBean.getResult().getLessontext();//這裏寫從後臺獲取富文本字符串
StringBuilder sb = new StringBuilder();
sb.append(getHtmlData(tv_html));
web_introduction.loadDataWithBaseURL(null,sb.toString(),"text/html", "utf-8", null);

getHtmlData()方法

/**
 * 富文本適配
 */
private String getHtmlData(String bodyHTML) {
    String head = "<head>"
                + "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\"> "
                + "<style>img{max-width: 100%; width:auto; height:auto;}</style>"
                + "</head>";
        return "<html>" + head + "<body>" + bodyHTML + "</body></html>";
    }

OK,Android WebView加載富文本並全屏適配齊活,歡迎評論指點,共同學習,共勉

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章