Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to load a page with PopWindow nested WebView

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article shows you how to use PopWindow nested WebView to load pages, the content is concise and easy to understand, can definitely brighten your eyes, through the detailed introduction of this article, I hope you can get something.

PopWindow has been used a lot before, trying to load a page with WebView for the first time. With WebView, you can easily embed web pages into APP and call each other directly with JS. Android Webview uses different webkit kernels in lower and higher versions, and Chrome is used directly after 4.4.

Several key points of setup

Set network access permissions in AndroidManifest.xml:

Layout file:

Call method:

WebView = (WebView) findViewById (R.id.webView)

/ / WebView loads web resources

WebView.loadUrl ("http://baidu.com");"

/ / override WebView's default behavior of opening a web page using a third party or the system's default browser, so that the web page is opened with WebView

WebView.setWebViewClient (new WebViewClient () {

@ Override

Public boolean shouldOverrideUrlLoading (WebView view, String url) {

/ / TODO Auto-generated method stub

/ / when the return value is true, control to go to WebView and open it, and call the system browser or third-party browser for false

View.loadUrl (url)

Return true

}

});

Several key points

/ / override the shouldOverrideUrlLoading () method so that the system browser is not called when the web page is opened, but is displayed in this WebView

MWebView.setWebViewClient (new WebViewClient () {

@ Override

Public boolean shouldOverrideUrlLoading (WebView view, String url) {

View.loadUrl (url)

Return true

}

}

/ / the property setBuiltInZoomControls below in popwindow must not be opened, or it will crash.

WebSettings.setBuiltInZoomControls (false); / / sets the built-in zoom control. If false, the WebView is not scalable

A self-encapsulated Popwindow opens the class of WebView:

Source code:

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.

* use PopWindow to load WebView to display web pages

, /

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) .predicate (R.layout.webview, null)

/ / initialize PopWindow

InitPopWindow ()

/ / initialize WebView

InitWebView (url)

}

Private static void InitWebView (String url) {

MWebView = (WebView) mView.findViewById (R.id.webview)

/ / load the page

MWebView.loadUrl (url)

/ / declare WebSettings subclass

WebSettings webSettings = mWebView.getSettings ()

/ / if you want to interact with Javascript on the page you visit, webview must be set to support Javascript

WebSettings.setJavaScriptEnabled (true)

/ / if JS is performing animation and other operations in the loaded html, it will result in a waste of resources (CPU, power)

/ / set setJavaScriptEnabled () to false and true in onStop and onResume, respectively.

/ / support plug-ins

WebSettings.setPluginState (WebSettings.PluginState.ON)

/ / set the adaptive screen, which can be used together.

WebSettings.setUseWideViewPort (true); / / resize the picture to fit the webview

WebSettings.setLoadWithOverviewMode (true); / / Zoom to the screen size

/ / Zoom operation

WebSettings.setSupportZoom (true); / / supports scaling. Default is true. It's the premise of the one below.

/ / the property setBuiltInZoomControls below in popwindow must not be opened, or it will crash.

WebSettings.setBuiltInZoomControls (false); / / sets the built-in zoom control. If false, the WebView is not scalable

WebSettings.setDisplayZoomControls (false); / / hides the native zoom control

/ / other details

WebSettings.setAppCacheEnabled (true); / / start caching

WebSettings.setCacheMode (WebSettings.LOAD_DEFAULT); / / disable caching in webview

WebSettings.setAllowFileAccess (true); / / set to access files

WebSettings.setJavaScriptCanOpenWindowsAutomatically (true); / / support to open a new window through JS

WebSettings.setLoadsImagesAutomatically (true); / / supports automatic loading of images

WebSettings.setDefaultTextEncodingName ("utf-8"); / / set the encoding format

/ / override the shouldOverrideUrlLoading () method so that the system browser is not called when the web page is opened, but is displayed in this WebView

MWebView.setWebViewClient (new WebViewClient () {

@ Override

Public boolean shouldOverrideUrlLoading (WebView view, String url) {

View.loadUrl (url)

Return true

}

/ / processing https requests webView does not process https requests by default, and the page is blank. You need to make the following settings:

@ Override

Public void onReceivedSslError (WebView view, SslErrorHandler handler, SslError error) {

Handler.proceed (); / / wait for the certificate to respond

/ / handler.cancel (); / / means to suspend the connection, which is the default mode

/ / handler.handleMessage (null); / / other processing can be done

}

});

MWebView.setWebChromeClient (new WebChromeClient ())

}

Private static void InitPopWindow () {

/ / get the screen resolution

DisplayMetrics metric = mContext.getResources () .getDisplayMetrics ()

/ / set the mPopupWindow size to 7/10 of the screen.

Int width = metric.widthPixels / 10 * 7; / / width (PX)

Int height = metric.heightPixels / 10 * 7; / / height (PX)

MPopupWindow = new PopupWindow (mView, width

Height)

MPopupWindow.setContentView (mView)

MPopupWindow.setTouchable (true)

/ / background must be set

MPopupWindow.setBackgroundDrawable (new PaintDrawable ())

/ / set focus

MPopupWindow.setFocusable (true)

/ / set the location where popupwindow appears

MPopupWindow.setOutsideTouchable (true)

MPopupWindow.showAtLocation (mView, Gravity.CENTER, 0,0)

/ / release webview when closed

MPopupWindow.setOnDismissListener (new Popup_Window.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 layout file:

Call method:

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

The above is how to load pages with PopWindow nested WebView. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report