我想将我的本地html文件显示到webview中。我使用此代码:
WebView wv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
wv=new WebView(this);
setContentView(wv);
try {
InputStream stream = this.getAssets().open("index.html");
int sreamsize = stream.available();
byte[] bufer = new byte[sreamsize];
stream.read(bufer);
stream.close();
String html= new String(bufer);
wv.loadData(html, "text/html", "UTF-8");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
现在我可以看到带有css和js效果的webview中的元素。怎么解决这个问题?
答案 0 :(得分:3)
试试这个: -
WebView wv;
wv = (WebView) findViewById(R.id.webView1);
WebSettings webViewSettings = wv.getSettings();
webViewSettings.setJavaScriptEnabled(true);
webViewSettings.setDomStorageEnabled(true);
wv.loadUrl("file:///android_asset/index.html");
答案 1 :(得分:2)
尝试替换
try {
InputStream stream = this.getAssets().open("index.html");
int sreamsize = stream.available();
byte[] bufer = new byte[sreamsize];
stream.read(bufer);
stream.close();
String html= new String(bufer);
wv.loadData(html, "text/html", "UTF-8");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
与
wv.loadUrl("file:///android_asset/index.html");
再试一次。
<强>更新强>
对于JS
来说,完美无缺
试试
WebSettings webViewSettings = wv.getSettings();
webViewSettings.setJavaScriptEnabled(true);
webViewSettings.setDomStorageEnabled(true);
@Sabari首选。
答案 2 :(得分:2)
试试这个: -
WebView wv;
wv = (WebView) findViewById(R.id.webView1);
wv.loadUrl("file:///android_asset/index.html");
答案 3 :(得分:1)
您是否尝试过wv.loadUrl("file:///android_asset/index.html");
?