1.引入GifView.jar,编写xml文件(为了让loading居中,不得不尝试了很多方法,结果就添加了好几个嵌套的layout才实现,如果有更简洁的方法,请博友告知):

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:background="@color/white"
  6. tools:context="com.doorknocker.MainActivity"
  7. android:orientation="vertical" >
  8. <LinearLayout
  9. android:id="@ id/ll_web_view_container"
  10. android:layout_width="match_parent"
  11. android:layout_height="match_parent" >
  12. <RelativeLayout
  13. android:id="@ id/rl_loading"
  14. android:layout_width="match_parent"
  15. android:layout_height="match_parent" >
  16. <LinearLayout
  17. android:layout_width="match_parent"
  18. android:layout_height="wrap_content"
  19. android:orientation="vertical"
  20. android:gravity="center_horizontal"
  21. android:layout_centerVertical="true" >
  22. <com.ant.liao.GifView android:id="@ id/gv_loading"
  23. android:layout_height="wrap_content"
  24. android:layout_width="wrap_content"
  25. android:enabled="false" />
  26. </LinearLayout>
  27. </RelativeLayout>
  28. <WebView
  29. android:id="@ id/wv_right_content"
  30. android:layout_width="match_parent"
  31. android:layout_height="match_parent" />
  32. </LinearLayout>
  33. </LinearLayout>
  1. 2.代码:
  2. View rootView = inflater.inflate(R.layout.fragment_right, null);
  3. wv_right_content = (WebView) rootView.findViewById(R.id.wv_right_content);
  4. gv_loading = (GifView) rootView.findViewById(R.id.gv_loading);
  5. rl_loading = (RelativeLayout) rootView.findViewById(R.id.rl_loading);
  6. gv_loading.setGifImage(R.drawable.loading_gif);
  7. // 得到底部菜单栏高度
  8. bottomTabHeight = UIUtils.getBottomTabHeight(mActivity);
  9. // WebView的高度是屏幕整体高度减去状态栏高度,再减去顶部菜单条高度,再减去底部Tab高度
  10. LinearLayout mLayout = (LinearLayout) rootView.findViewById(R.id.ll_web_view_container);
  11. Layoutparams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
  12. params.height = UIUtils.getScreenHeight(mActivity) - UIUtils.getStatusBarHeight() - bottomTabHeight;
  13. mLayout.setLayoutParams(params);
  14. WebSettings webSettings = wv_right_content.getSettings();
  15. webSettings.setJavaScriptEnabled(true);
  16. webSettings.setSupportZoom(true);
  17. webSettings.setBuiltInZoomControls(true);
  18. //设置Web视图
  19. wv_right_content.setWebViewClient(new WebViewClient() {
  20. @Override
  21. public boolean shouldOverrideUrlLoading(WebView view, String url) {
  22. view.loadUrl(url);
  23. // 记住当前的URL
  24. currentRightUrl = url;
  25. // 由当前的WebView处理跳转
  26. return true;
  27. }
  28. @Override
  29. public void onPageStarted(WebView view, String url, Bitmap favicon) {
  30. rl_loading.setVisibility(View.VISIBLE);
  31. wv_right_content.setVisibility(View.GONE);
  32. }
  33. @Override
  34. public void onPageFinished(WebView view, String url) {
  35. super.onPageFinished(view, url);
  36. rl_loading.setVisibility(View.GONE);
  37. wv_right_content.setVisibility(View.VISIBLE);
  38. }
  39. });
  40. return rootView;

webview提示加载进度(WebView在加载完成之前显示loading图标)(1)

,