如何用PopWindow嵌套WebView加载页面

发布时间:2022-01-04 18:09:10 作者:柒染
来源:亿速云 阅读:164

本篇文章为大家展示了如何用PopWindow嵌套WebView加载页面,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

PopWindow以前用了不少,第一次尝试用WebView加载页面.用WebView可以轻松实现网页内嵌到APP里,还可以直接跟JS相互调用.Android的Webview在低版本和高版本采用了不同的webkit版本内核,4.4后直接使用了Chrome。

几个设置要点

在AndroidManifest.xml设置访问网络权限:

如何用PopWindow嵌套WebView加载页面

布局文件:

如何用PopWindow嵌套WebView加载页面

调用方法:

        webView = (WebView) findViewById(R.id.webView);
        //WebView加载web资源
       webView.loadUrl("http://baidu.com");
        //覆盖WebView默认使用第三方或系统默认浏览器打开网页的行为,使网页用WebView打开
       webView.setWebViewClient(new WebViewClient(){
           @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            // TODO Auto-generated method stub
               //返回值是true的时候控制去WebView打开,为false调用系统浏览器或第三方浏览器
             view.loadUrl(url);
            return true;
        }
       });

几个要点

        //复写shouldOverrideUrlLoading()方法,使得打开网页时不调用系统浏览器, 而是在本WebView中显示
        mWebView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

        }

        //在popwindow里面下面setBuiltInZoomControls这个属性一定不能打开,否则崩溃
        webSettings.setBuiltInZoomControls(false); //设置内置的缩放控件。若为false,则该WebView不可缩放

自己封装的一个Popwindow打开WebView的类:

源码:

package scm.vaccae.basemodule;

import android.content.Context;
import android.graphics.drawable.PaintDrawable;
import android.net.http.SslError;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.PopupWindow;

/**
 * Created by Administrator on 2017-11-12.
 * 用PopWindow来加载WebView显示网页
 */

public class ShowWebView {

    private static Context mContext;
    private static View mView;
    private static PopupWindow mPopupWindow;
    private static WebView mWebView;

    public static void show(Context context, String url) {
        mContext = context;
        mView = LayoutInflater.from(mContext).inflate(R.layout.webview, null);
        //初始化PopWindow
        InitPopWindow();
        //初始化WebView
        InitWebView(url);
    }

    private static void InitWebView(String url) {
        mWebView = (WebView) mView.findViewById(R.id.webview);
        //加载页面
        mWebView.loadUrl(url);
        //声明WebSettings子类
        WebSettings webSettings = mWebView.getSettings();
        //如果访问的页面中要与Javascript交互,则webview必须设置支持Javascript
        webSettings.setJavaScriptEnabled(true);
        // 若加载的 html 里有JS 在执行动画等操作,会造成资源浪费(CPU、电量)
        // 在 onStop 和 onResume 里分别把 setJavaScriptEnabled() 给设置成 false 和 true 即可
        //支持插件
        webSettings.setPluginState(WebSettings.PluginState.ON);
        //设置自适应屏幕,两者合用
        webSettings.setUseWideViewPort(true); //将图片调整到适合webview的大小
        webSettings.setLoadWithOverviewMode(true); // 缩放至屏幕的大小
        //缩放操作
        webSettings.setSupportZoom(true); //支持缩放,默认为true。是下面那个的前提。
        //在popwindow里面下面setBuiltInZoomControls这个属性一定不能打开,否则崩溃
        webSettings.setBuiltInZoomControls(false); //设置内置的缩放控件。若为false,则该WebView不可缩放
        webSettings.setDisplayZoomControls(false); //隐藏原生的缩放控件
        //其他细节操作
        webSettings.setAppCacheEnabled(true);  //启动缓存
        webSettings.setCacheMode(WebSettings.LOAD_DEFAULT); //关闭webview中缓存
        webSettings.setAllowFileAccess(true); //设置可以访问文件
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true); //支持通过JS打开新窗口
        webSettings.setLoadsImagesAutomatically(true); //支持自动加载图片
        webSettings.setDefaultTextEncodingName("utf-8");//设置编码格式

        //复写shouldOverrideUrlLoading()方法,使得打开网页时不调用系统浏览器, 而是在本WebView中显示
        mWebView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

            //处理https请求 webView默认是不处理https请求的,页面显示空白,需要进行如下设置:
            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed();    //表示等待证书响应
                // handler.cancel();      //表示挂起连接,为默认方式
                // handler.handleMessage(null);    //可做其他处理
            }
        });
        mWebView.setWebChromeClient(new WebChromeClient());
    }

    private static void InitPopWindow() {
        //获取屏幕分辨率
        DisplayMetrics metric = mContext.getResources().getDisplayMetrics();
        //设置mPopupWindow大小按屏幕的十分之七来算
        int width = metric.widthPixels / 10 * 7;  // 宽度(PX)
        int height = metric.heightPixels / 10 * 7;  // 高度(PX)

        mPopupWindow = new PopupWindow(mView, width,
                height);
        mPopupWindow.setContentView(mView);
        mPopupWindow.setTouchable(true);
        //必须设置背景
        mPopupWindow.setBackgroundDrawable(new PaintDrawable());
        //设置焦点
        mPopupWindow.setFocusable(true);
        //设置popupwindow出现的位置
        mPopupWindow.setOutsideTouchable(true);
        mPopupWindow.showAtLocation(mView, Gravity.CENTER, 0, 0);

        //关闭时释放webview
        mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                if (mWebView != null) {
                    mWebView.loadDataWithBaseURL(null, "", "text/html", "utf-8", null);
                    mWebView.clearHistory();

                    ((ViewGroup) mWebView.getParent()).removeView(mWebView);
                    mWebView.destroy();
                    mWebView = null;
                }
                System.gc();
            }
        });
    }
}

XML布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webview_layout"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <WebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

调用方法:

ShowWebView.show(this, "http://www.baidu.com");

上述内容就是如何用PopWindow嵌套WebView加载页面,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. WebView加载网页(二)
  2. WebView加载网页(一)

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

popwindow webview

上一篇:如何进行数组类型与数组指针的巧妙利用

下一篇:ShutdownHook如何让应用优雅的停止

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》