1.创建一个工具类
public class WebFont {
public static void showWebFont(WebView myWebView, String content) {
showWebFont(myWebView, content, 16);
}
public static void showWebFont(WebView myWebView, String content,
int fontSize) {
showWebFont(myWebView, content, fontSize, "#323232");
}
public static void showWebFont(WebView myWebView, String content,
int fontSize, String fontColor) {
showWebFont(myWebView, content, fontSize, fontColor, "#fff8f7");
}
public static void showWebFont(WebView myWebView, String content,
int fontSize, String fontColor , String bg , String a) {
showWebFont(myWebView, content, fontSize, fontColor, bg);
}
/**
* 显示文字
*
* @param myWebView
* 显示文字的webview
* @param content
* 显示的文字
* @param fontSize
* 文字大小
* @param fontColor
* 文字颜色
* @param backgroundColor
* 显示文字的背景
*/
public static void showWebFont(WebView myWebView, String content,
int fontSize, String fontColor, String backgroundColor) {
// 设置webview不允许竖直滚动
myWebView.setVerticalScrollBarEnabled(false);
//backgroundColor = "#fff8f7";
myWebView.loadDataWithBaseURL("",
"<![CDATA[<html> <head></head> <body style=\"background-color:"
+ backgroundColor + ";text-align:justify;font-size:"
+ fontSize + "px;color:" + fontColor
+ ";\"> " + content
+ " <p> </body></html>", "text/html", "utf-8", "");
}
使用直接调用就可以了,
WebFont webFont = new WebFont();
webFont.showWebFont(mTongzhiNei,h5);
3.也可以通过WebSetting对WeiView进行一些修改
WebSettings webSetting = mWebView.getSettings();
webSetting.setJavaScriptEnabled(true);
webSetting.setJavaScriptCanOpenWindowsAutomatically(true);
webSetting.setAllowFileAccess(true);
webSetting.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
webSetting.setSupportZoom(true);
webSetting.setBuiltInZoomControls(true);
webSetting.setUseWideViewPort(true);
webSetting.setSupportMultipleWindows(true);
// webSetting.setLoadWithOverviewMode(true);
webSetting.setAppCacheEnabled(true);
webSetting.setDatabaseEnabled(true);
webSetting.setDomStorageEnabled(true);
webSetting.setGeolocationEnabled(true);
webSetting.setAppCacheMaxSize(Long.MAX_VALUE);
// webSetting.setPageCacheCapacity(IX5WebSettings.DEFAULT_CACHE_CAPACITY);
webSetting.setPluginState(WebSettings.PluginState.ON_DEMAND);
// webSetting.setRenderPriority(WebSettings.RenderPriority.HIGH);
webSetting.setCacheMode(WebSettings.LOAD_NO_CACHE);
webSetting.setSaveFormData(false);
mWebView.setWebViewClient(mWebViewClient);
4.也可以在Application里对WebView开启X5内核进行加速处理
QbSdk.PreInitCallback cb = new QbSdk.PreInitCallback() {
//x5內核初始化完成的回调,为true表示x5内核加载成功,否则表示x5内核加载失败,会自动切换到系统内核。
@Override
public void onViewInitFinished(boolean arg0) {
Log.d(LogTag, " onViewInitFinished is " + arg0);
}
@Override
public void onCoreInitFinished() {
Log.d(LogTag, "onCoreInitFinished...");
}
};
QbSdk.initX5Environment(getApplicationContext(), cb); //x5内核初始化接口
Log.d(LogTag, "APPAplication onCreate finish...");