Android用文本view加载HTML时,可以首先获取到html的内容,然后对html做自己想要的效果调整
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.web);
loadHtml();
}
private void loadHtml() {
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setSupportZoom(true);
webView.addJavascriptInterface(new InJavaScriptLocalObj(), "java_obj");
webView.loadUrl("http://www.baidu.com");
webView.setWebViewClient(new CustomWebViewClient());
WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
int width = wm.getDefaultDisplay().getWidth();
webView.scrollTo(width, 0);
}
final class CustomWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
@Override
public void onPageFinished(WebView view, String url) {
view.loadUrl("javascript:window.java_obj.getSource('
"document.getElementsByTagName('html')[0].innerHTML+'');");
super.onPageFinished(view, url);
}
}
final class InJavaScriptLocalObj {
@JavascriptInterface
public void getSource(String html) {
Log.e("html=", html);
}
}