AgentWeb 是一个基于的 Android WebView ,极度容易使用以及功能强大的库,极好用的第三方的库,能够解决andoird自带webview加载网页不稳定的因素

本来想用腾讯的TBS的X5内核库加载网页。但是想想还是用这个。
//AgentWeb 是一个基于的 Android WebView ,极度容易使用以及功能强大的库
implementation ‘com.just.agentweb:agentweb:4.1.3’

布局文件如下,这里采用线性布局

            <LinearLayout
                android:id="@+id/web"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="5dp"
                android:layout_marginBottom="15dp"
                android:orientation="vertical" />
    public static void webview_type(String s,AgentWeb agentWeb,Activity activity,View web){
    
    
        com.just.agentweb.WebViewClient mWebViewClient=new com.just.agentweb.WebViewClient(){
    
    
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
    
    
                super.onPageStarted(view, url, favicon);
                //     if (urlStr.equals(url)) {
    
    
                //  image_back.setVisibility(View.GONE);

                //     } else {
    
    
                // image_back.setVisibility(View.VISIBLE);

            }
        };

        //访问网页
    //    String s=goodsDetailModel.getMeta().getDesc_html_url();
        Log.i("TAG", "打印地址: "+s);
        if(!s.isEmpty()) {
    
    
            agentWeb = AgentWeb.with(activity)//
                    .setAgentWebParent((LinearLayout) web, -1, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT))//传入AgentWeb的父控件。
                    .useDefaultIndicator(-1, 3)//设置进度条颜色与高度,-1为默认值,高度为2,单位为dp。
                    .setSecurityType(AgentWeb.SecurityType.STRICT_CHECK) //严格模式 Android 4.2.2 以下会放弃注入对象 ,使用AgentWebView没影响。
                 //   .setWebViewClient(mWebViewClient)//WebViewClient , 与 WebView 使用一致 ,但是请勿获取WebView调用setWebViewClient(xx)方法了,会覆盖AgentWeb DefaultWebClient,同时相应的中间件也会失效。
                    .setMainFrameErrorView(R.layout.agentweb_error_page, -1) //参数1是错误显示的布局,参数2点击刷新控件ID -1表示点击整个布局都刷新, AgentWeb 3.0.0 加入。
                    .setOpenOtherPageWays(DefaultWebClient.OpenOtherPageWays.DISALLOW)//打开其他页面时,弹窗质询用户前往其他应用 AgentWeb 3.0.0 加入。
                    .interceptUnkownUrl() //拦截找不到相关页面的Url AgentWeb 3.0.0 加入。
                    .createAgentWeb()//创建AgentWeb。
                    .ready()//设置 WebSettings。
                    .go(s); //WebView载入该url地址的页面并显示。
        }


    }

这是我自己封装的一个方法,可以往里面传几个参数,最重要的是要传加载网页的线性布局的控件id:web。加载网页的时候会有默认的进度条。

猜你喜欢

转载自blog.csdn.net/qq_36570506/article/details/105110585