1.布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="net.pericles.andoidab.muiaps.MainActivity">
<WebView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/wevsss"></WebView>
</RelativeLayout>
2.权限设置
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.pericles.andoidab.muiaps">
<!--添加权限-->
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
3.java代码实现
private WebView webViews;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webViews = (WebView)findViewById(R.id.wevsss);
webViews.loadUrl("http://www.baidu.com");
webViews.getSettings().setLoadWithOverviewMode(true);
webViews.getSettings().setUseWideViewPort(true);
webViews.getSettings().setJavaScriptEnabled(true);
webViews.setWebViewClient(new WebViewClient()
{
public boolean shouldOvertideUrlLoading(WebView view ,String url)
{
view.loadUrl(url);
return super.shouldOverrideUrlLoading(view,url);
}
});
}
4.注意
<!--添加权限-->
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
5.细节
、
今天在做项目时,发现webview加载的url太大,导致显示上有点畸形,所以想webview最好能自适应屏幕,于是乎baidu,google,webview还真给我们提供了这种方法,如下代码:
webViews.getSettings().setLoadWithOverviewMode(true);
webViews.getSettings().setUseWideViewPort(true);