存放本地html文件:放在app/src/main/assets目录下,一般来说android studio项目下是没有assets文件夹的,所以需要在app/src/main下新建一个文件夹,取名assets;详细步骤请看:[(https://blog.csdn.net/qq_36109644/article/details/82453909)
public class Main4Activity extends AppCompatActivity {
private WebView webView;
private ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);
progressBar= (ProgressBar)findViewById(R.id.progressbar1);//进度条
webView = (WebView) findViewById(R.id.webview1);
webView.loadUrl("file:///android_asset/header.html");//加载本地asset文件夹下html
webView.setWebChromeClient(webChromeClient);
webView.setWebViewClient(webViewClient);
}
//WebViewClient主要帮助WebView处理各种通知、请求事件
private WebViewClient webViewClient=new WebViewClient(){
@Override
public void onPageFinished(WebView view, String url) {//页面加载完成
progressBar.setVisibility(View.GONE);
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {//页面开始加载
progressBar.setVisibility(View.VISIBLE);
}
};
//WebChromeClient主要辅助WebView处理Javascript的对话框、网站图标、网站title、加载进度等
private WebChromeClient webChromeClient=new WebChromeClient(){
@Override
public void onProgressChanged(WebView view, int newProgress) {
progressBar.setProgress(newProgress);
}
};
@Override
protected void onDestroy() {
super.onDestroy();
//资源释放
webView.destroy();
webView=null;
}
}
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns: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/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<ProgressBar
android:id="@+id/progressbar"
style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="3dip"
android:max="100"
android:progress="50"//进度条当前值
android:visibility="gone"/>
</FrameLayout>
网友评论