您可以为此使用 WebChrmeClient,无需进行任何 hack,下面的代码将适用于 HTML 以及 URl 和 css,请看一下:
public class IjoomerWebviewClient extends Activity {
private WebView webExternalLinks;
private IjoomerButton btnClose;
private String link = "";
private String IN_CONTENT = "";
SeekBar seekBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.YOUR_WEBVIEW_LAYOUT);
webExternalLinks = (WebView) findViewById(R.id.webExternalLinks);
btnClose = (IjoomerButton) findViewById(R.id.btnClose);
getData();
}
private void getData(){
getIntentData();
webExternalLinks.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
if(seekBar==null){
seekBar = IjoomerUtilities
.getLoadingDialog(getString(R.string.dialog_loading_please_wait));
}
seekBar.setProgress(progress);
if(progress==100){
seekBar=null;
}
}
});
webExternalLinks.setWebViewClient(new WebViewClient());
webExternalLinks.getSettings().setJavaScriptEnabled(true);
webExternalLinks.getSettings().setPluginState(PluginState.ON);
webExternalLinks.getSettings().setSupportZoom(true);
webExternalLinks.getSettings().setBuiltInZoomControls(true);
if (link != null && link.length() > 0) {
if (!link.startsWith("http://") && !link.startsWith("https://")) {
link = "http://" + link;
}
webExternalLinks.loadUrl(link);
} else if (IN_CONTENT != null && IN_CONTENT.trim().length() > 0) {
StringBuilder sb = new StringBuilder(); // StringBuilder();
sb.append("<HTML><HEAD><link rel=\"stylesheet\" type=\"text/css\" href=\"weblayout.css\" /></HEAD><body>");
String str = IN_CONTENT.trim();
str = str.replaceAll("<iframe width=\"[0-9]*",
"<iframe width=\"100\\%");
str = str.replaceAll("<img[\\w]*",
"<img height=\"auto\" style=\"max-width:100\\%\";");
sb.append(str);
sb.append("</body></HTML>");
webExternalLinks.loadDataWithBaseURL("file:///android_asset/css/",
sb.toString(), "text/html", "utf-8", null);
}
}
private void getIntentData() {
try {
link = this.getIntent().getStringExtra("url");
if (link == null) {
IN_CONTENT = this.getIntent().getStringExtra("IN_CONTENT");
}
} catch (Exception e) {
IN_CONTENT = this.getIntent().getStringExtra("IN_CONTENT");
}
}
@Override
public void onBackPressed() {
if(webExternalLinks.canGoBack()){
webExternalLinks.goBack();
}else{
super.onBackPressed();
}
}
}