WebView設定文字內容字型的大小以及顏色
阿新 • • 發佈:2019-02-15
改變 WebView上顯示網頁內容的CSS樣式:
//字型顏色設為白色, “p”標籤內的字型顏色 “*”定義了字型大小以及行高;
public final static String CSS_STYLE ="<style>* {font-size:16px;line-height:20px;}p {color:#FFFFFF;}</style>";
//data是要顯示的內容
webView.loadDataWithBaseURL(null, CSS_STYLE+data, "text/html","utf-8", null);
比較全的:
public final static String CSS_STYLE ="<style>* {font-size:16px;line-height:20px;} p {color:#333;} a {color:#3E62A6;} img {max-width:310px;}pre {font-size:9pt;line-height:12pt;font-family:Courier New,Arial;border:1px solid #ddd;border-left:5px solid #6CE26C;background:#f6f6f6;padding:5px;}</style> ";上面全域性樣式:“*”定義了字型大小以及行高;“p”標籤內的字型顏色;“a”標籤內的字型顏色;“img”標籤的圖片最大寬度;“pre”為程式碼樣式;------------------------------------------------------------------------------------------------------------------------------------------------------
借鑑自:http://www.android100.org/html/201306/25/3285.html
其他:
資訊內容是由服務返回的一串帶HTML標籤的字串:
相關資訊則是由服務返回的資料組裝的:
WebView上顯示圖片,不能直接顯示大圖,這會影響頁面的美觀以及使用者體驗,因此要過濾掉原始圖片的高寬屬性,使用全域性的圖片樣式。同時客戶端可以根據使用者設定,是否載入圖片顯示,以達到節省流量的目的。
WebView展示HTML
//字型顏色設為白色, “p”標籤內的字型顏色 “*”定義了字型大小以及行高;
public final static String CSS_STYLE ="<style>* {font-size:16px;line-height:20px;}p {color:#FFFFFF;}</style>";
//data是要顯示的內容
webView.loadDataWithBaseURL(null, CSS_STYLE+data, "text/html","utf-8", null);
比較全的:
public final static String CSS_STYLE ="<style>* {font-size:16px;line-height:20px;} p {color:#333;} a {color:#3E62A6;} img {max-width:310px;}pre {font-size:9pt;line-height:12pt;font-family:Courier New,Arial;border:1px solid #ddd;border-left:5px solid #6CE26C;background:#f6f6f6;padding:5px;}</style>
借鑑自:http://www.android100.org/html/201306/25/3285.html
其他:
資訊內容是由服務返回的一串帶HTML標籤的字串:
String body = newsDetail.getBody();
String strRelative = ""; for(Relative relative : newsDetail.getRelatives()){ strRelative += String.format("%s", relative.url, relative.title); }圖片處理
WebView上顯示圖片,不能直接顯示大圖,這會影響頁面的美觀以及使用者體驗,因此要過濾掉原始圖片的高寬屬性,使用全域性的圖片樣式。同時客戶端可以根據使用者設定,是否載入圖片顯示,以達到節省流量的目的。
if(isLoadImage){ //過濾掉 img標籤的width,height屬性 body = body.replaceAll("(]*?)\\s+width\\s*=\\s*\\S+","$1"); body = body.replaceAll("(]*?)\\s+height\\s*=\\s*\\S+","$1"); }else{ //過濾掉 img標籤 body = body.replaceAll("<\\s*img\\s+([^>]*)\\s*>",""); }
mWebView.loadDataWithBaseURL(null, body, "text/html", "utf-8",null);
-----------------------------------------------------------------------------------------
其它方法:WebView webview= (WebView) findViewById(R.id.webview); WebSettings settings=webview.getSettings(); settings.setTextSize(WebSettings.TextSize.SMALLEST); settings.setTextSize(WebSettings.TextSize.SMALLER); settings.setTextSize(WebSettings.TextSize.NORMAL); settings.setTextSize(WebSettings.TextSize.LARGER); settings.setTextSize(WebSettings.TextSize.LARGEST);