首先在布局xml里面指定WebView根节点
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[http://schemas.android.com/apk/res/android](http://schemas.android.com/apk/res/android)"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<WebView
android:id="@+id/web_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
在.java的onCreate()里使用
private WebView webview;
private String filePath;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webview);
webview = findViewById(R.id.web_content);
initWebView();
//1. asset目录下的index.html文件
filePath = "file:///android_asset/index.html";
//2.本地sd卡内的index.html文件
filePath = "content://com.android.htmlfileprovider/sdcard/index.html";
//3.指定的URL的html文件
filePath = "http://wap.baidu.com";
//加载html
if (filePath != null) {
webview.loadUrl(filePath);
}
}
private void initWebView()
{
WebSettings settings = mContentWv.getSettings();
//设置支持缩放
settings.setBuiltInZoomControls(true);
// 缩放至屏幕的大小
settings.setLoadWithOverviewMode(true);
settings.setJavaScriptEnabled(true);
}
网友评论