昨天使用webview在移动端加载网页,遇到一些小问题,在这里记录下来。
第一,要使你的网页不显示应用标题,可以在manifest.xml总的某个activity中声明android:theme="@android:style/Theme.NoTitleBar",也就是设置这个activity的主题为无标题模式。
第二,感觉系统的webview不是很好用,有时候出现页面显示不符合要求的情况,这里使用了腾讯提供的webview空间,感觉效果要好一点,使用的方法也很简单,首先下载引用控件jar包,在xml文件中引用控件,
<com.tencent.smtt.sdk.WebView
android:id="@+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
然后在需要引入的地方把import的包换成
import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;
就可以了。
第三,要使用js代码对HTML页面进行控制,要改变页面的视窗,最好进行如下设置,这样控制起来就比较灵活了。
WebSettings webSettings = chromeView.getSettings();
webSettings.setJavaScriptEnabled(true); //设置这个很重要,不设置的话很多h5页面会显示异常
webSettings.setUseWideViewPort(true);//设置此属性,可任意比例缩放
webSettings.setLoadWithOverviewMode(true);
第四、webview中js alert对话框 不能弹出 解决办法
首先设置容许弹出框:
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);//允许js弹出窗口
其次:
webview只是一个承载体,各种内容的渲染需要使用webviewChromClient去实现,所以set一个默认的基类WebChromeClient就行,代码如下:
chromeView.setWebChromeClient(new WebChromeClient());
第五,android里面 使用html5的 localStorage
要进行如下设置:
mWebView.getSettings().setDomStorageEnabled(true);
mWebView.getSettings().setAppCacheMaxSize(1024*1024*8);
String appCachePath = getApplicationContext().getCacheDir().getAbsolutePath();
mWebView.getSettings().setAppCachePath(appCachePath);
mWebView.getSettings().setAllowFileAccess(true);
mWebView.getSettings().setAppCacheEnabled(true);