WebView在加载完成之前显示loading图标

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;


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ithouse

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值