1.引入GifView.jar,编写xml文件(为了让loading居中,不得不尝试了很多方法,结果就添加了好几个嵌套的layout才实现,如果有更简洁的方法,请博友告知):
<LinearLayout 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"
android:background="@color/white"
tools:context="com.doorknocker.MainActivity"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/ll_web_view_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:id="@+id/rl_loading"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_centerVertical="true" >
<com.ant.liao.GifView android:id="@+id/gv_loading"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:enabled="false" />
</LinearLayout>
</RelativeLayout>
<WebView
android:id="@+id/wv_right_content"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</LinearLayout>
2.代码:
View rootView = inflater.inflate(R.layout.fragment_right, null);
wv_right_content = (WebView) rootView.findViewById(R.id.wv_right_content);
gv_loading = (GifView) rootView.findViewById(R.id.gv_loading);
rl_loading = (RelativeLayout) rootView.findViewById(R.id.rl_loading);
gv_loading.setGifImage(R.drawable.loading_gif);
// 得到底部菜单栏高度
bottomTabHeight = UIUtils.getBottomTabHeight(mActivity);
// WebView的高度是屏幕整体高度减去状态栏高度,再减去顶部菜单条高度,再减去底部Tab高度
LinearLayout mLayout = (LinearLayout) rootView.findViewById(R.id.ll_web_view_container);
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.height = UIUtils.getScreenHeight(mActivity) - UIUtils.getStatusBarHeight() - bottomTabHeight;
mLayout.setLayoutParams(params);
WebSettings webSettings = wv_right_content.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setSupportZoom(true);
webSettings.setBuiltInZoomControls(true);
//设置Web视图
wv_right_content.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
// 记住当前的URL
currentRightUrl = url;
// 由当前的WebView处理跳转
return true;
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
rl_loading.setVisibility(View.VISIBLE);
wv_right_content.setVisibility(View.GONE);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
rl_loading.setVisibility(View.GONE);
wv_right_content.setVisibility(View.VISIBLE);
}
});
return rootView;