WebView 文档 翻译
生活随笔
收集整理的這篇文章主要介紹了
WebView 文档 翻译
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文檔地址:https://developer.android.google.cn/reference/android/webkit/WebView.html
Note that, in order for your Activity to access the Internet and load web pages in a WebView, you must add the INTERNET permissions to your Android Manifest file請注意,為了讓您的Activity?訪問Internet并在WebView中加載網頁,您必須將INTERNET權限添加到AndroidManifest文件中
For more information, read Building Web Apps in WebView.
To provide a WebView in your own Activity, include a <WebView> in your layout, or set the entire Activity window as a WebView during onCreate():setContentView(new WebView(this));
Then load the desired web page:// Simplest usage: note that an exception will NOT be thrown if there is an error loading this page webview.loadUrl("https://example.com/");// OR, you can also load from an HTML string: String summary = "<html><body>You scored <b>192</b> points.</body></html>"; webview.loadData(summary, "text/html", null); // ... although note that there are restrictions約束、限制 on what this HTML can do. // See the JavaDocs for loadData() and loadDataWithBaseURL() for more info.
A WebView has several customization points where you can add your own behavior. These are:WebView有幾個自定義點,您可以添加自己的行為。 這些是:
Here's a more complicated example, showing error handling, settings, and progress notification:這是一個更復雜的例子,顯示錯誤處理,設置和進度通知:// Let's display the progress in the activity title bar, like the browser app does. getWindow().requestFeature(Window.FEATURE_PROGRESS);webview.getSettings().setJavaScriptEnabled(true);final Activity activity = this; webview.setWebChromeClient(new WebChromeClient() {public void onProgressChanged(WebView view, int progress) {// Activities and WebViews measure progress with different scales.// The progress meter計量儀 will automatically disappear when we reach 100%activity.setProgress(progress * 1000);} }); webview.setWebViewClient(new WebViewClient() {public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();} });webview.loadUrl("https://developer.android.com/");
By default, requests by the HTML to open new windows are ignored. This is true whether they be opened by JavaScript or by the target attribute on a link. You can customize your WebChromeClient to provide your own behavior for opening multiple windows, and render them in whatever manner you want.默認情況下,HTML的打開新窗口的請求將被忽略。 無論是通過JavaScript打開還是鏈接上的target屬性,都是如此。 您可以自定義WebChromeClient以提供您自己的打開多個窗口的行為,并以任何您想要的方式呈現它們。
The standard behavior for an Activity is to be destroyed and recreated when the device orientation or any other configuration changes. This will cause the WebView to reload the current page. If you don't want that, you can set your Activity to handle the orientation and keyboardHidden changes, and then just leave the WebView alone. It'll automatically re-orient itself as appropriate. Read Handling Runtime Changes for more information about how to handle configuration changes during runtime.當設備方向或任何其他配置更改時,Activity的標準行為是將被銷毀并重新創建。 這將導致WebView重新加載當前頁面。 如果你不想要這樣做,你可以設置你的Activity來處理方向和鍵盤的隱藏變化,然后就不需要再管WebView了。 它將自動適當的重新定位自己。 有關如何在運行時處理配置更改的更多信息,請閱讀"處理運行時更改"。
By default, WebView scales a web page so that it is drawn at a size that matches the default appearance on a medium density screen. So, it applies 1.5x scaling on a high density screen (because its pixels are smaller) and 0.75x scaling on a low density screen (because its pixels are bigger). Starting with API level ECLAIR, WebView supports DOM, CSS, and meta tag features to help you (as a web developer) target screens with different screen densities.默認情況下,WebView會縮放網頁,以使其繪制的尺寸與中密度屏幕上的默認外觀相匹配。因此,它在高密度屏幕上(因為其像素較小)使用1.5倍縮放,在低密度屏幕上(因為其像素較大)使用0.75倍縮放。從API ECLAIR開始,WebView支持DOM,CSS和元標記功能,可幫助您(作為Web開發人員)以不同的屏幕密度來定位屏幕。
Here's a summary of the features you can use to handle different screen densities:以下是可用于處理不同屏幕密度的功能的摘要:
Setting the WebView's 【height】 to WRAP_CONTENT enables the following behaviors:將WebView的高度設置為WRAP_CONTENT將導致以下行為:
Using a layout 【width】 of WRAP_CONTENT is not supported. If such a width is used the WebView will attempt to use the width of the parent instead.
使用WRAP_CONTENT作為布局寬度是不支持的。 如果使用了這樣的寬度,WebView會嘗試使用父布局的寬度來代替。
2017-8-17
來自為知筆記(Wiz)
WebView 文檔
public class android.webkit.WebView extends AbsoluteLayout?implements ViewTreeObserver.OnGlobalFocusChangeListener, ViewGroup.OnHierarchyChangeListenerjava.lang.Object? android.view.View? android.view.ViewGroup? android.widget.AbsoluteLayout? android.webkit.WebViewA View that displays web pages. This class is the basis upon which you can roll your own web browser or simply display some online content within your Activity. It uses the WebKit rendering engine to display web pages and includes methods to navigate forward and backward through a history, zoom in and out, perform text searches and more.顯示網頁的視圖。 這個類是您可以滾動自己的網絡瀏覽器,或只是在您的Activity中顯示一些在線內容的基礎。 它使用WebKit渲染引擎來顯示網頁,并且包括通過歷史記錄,放大和縮小,執行文本搜索等進行前進后退導航的方法。Note that, in order for your Activity to access the Internet and load web pages in a WebView, you must add the INTERNET permissions to your Android Manifest file請注意,為了讓您的Activity?訪問Internet并在WebView中加載網頁,您必須將INTERNET權限添加到AndroidManifest文件中
For more information, read Building Web Apps in WebView.
1、Basic usage
By default, a WebView provides no browser-like widgets, does not enable JavaScript and web page errors are ignored. If your goal is only to display some HTML as a part of your UI, this is probably fine; the user won't need to interact with the web page beyond reading it, and the web page won't need to interact with the user. If you actually want a full-blown web browser, then you probably want to invoke the Browser application with a URL Intent rather than show it with a WebView. For example:默認情況下,WebView不提供類似瀏覽器的小部件,不啟用JavaScript,網頁錯誤也將被忽略。 如果你的目標只是顯示一些HTML作為你的UI的一部分,這可能是很好的;用戶不需要在閱讀之前與網頁進行交互,網頁也不需要與用戶進行交互。 如果您真的想要一個完整的Web瀏覽器,那么您可能想要使用URL Intent調用瀏覽器應用程序,而不是使用WebView來顯示。 例如:startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.baidi.com")));To provide a WebView in your own Activity, include a <WebView> in your layout, or set the entire Activity window as a WebView during onCreate():setContentView(new WebView(this));
Then load the desired web page:// Simplest usage: note that an exception will NOT be thrown if there is an error loading this page webview.loadUrl("https://example.com/");// OR, you can also load from an HTML string: String summary = "<html><body>You scored <b>192</b> points.</body></html>"; webview.loadData(summary, "text/html", null); // ... although note that there are restrictions約束、限制 on what this HTML can do. // See the JavaDocs for loadData() and loadDataWithBaseURL() for more info.
A WebView has several customization points where you can add your own behavior. These are:WebView有幾個自定義點,您可以添加自己的行為。 這些是:
- Creating and setting a WebChromeClient subclass. This class is called when something that might impact a browser UI happens, for instance, progress updates and JavaScript alerts are sent here.
創建和設置WebChromeClient子類。 當可能影響瀏覽器UI的事情發生時,調用此類,例如,進度更新和JavaScript警報在此發送。 - Creating and setting a WebViewClient subclass. It will be called when things happen that impact the rendering of the content, eg, errors or form submissions. You can also intercept URL loading here (via shouldOverrideUrlLoading()).
創建和設置WebViewClient子類。 當事情發生時影響內容的呈現,例如錯誤或表單提交將被調用。 你也可以在這里攔截URL加載。 - Modifying the WebSettings, such as enabling JavaScript with setJavaScriptEnabled().
修改WebSettings,例如使用setJavaScriptEnabled啟用JavaScript。 - Injecting Java objects into the WebView using the addJavascriptInterface(Object, String) method. This method allows you to inject Java objects into a page's JavaScript context, so that they can be accessed by JavaScript in the page.
使用addJavascriptInterface方法將Java對象注入WebView。 此方法允許您將Java對象注入到頁面的JavaScript上下文中,以便可以通過頁面中的JavaScript訪問它們。
Here's a more complicated example, showing error handling, settings, and progress notification:這是一個更復雜的例子,顯示錯誤處理,設置和進度通知:// Let's display the progress in the activity title bar, like the browser app does. getWindow().requestFeature(Window.FEATURE_PROGRESS);webview.getSettings().setJavaScriptEnabled(true);final Activity activity = this; webview.setWebChromeClient(new WebChromeClient() {public void onProgressChanged(WebView view, int progress) {// Activities and WebViews measure progress with different scales.// The progress meter計量儀 will automatically disappear when we reach 100%activity.setProgress(progress * 1000);} }); webview.setWebViewClient(new WebViewClient() {public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();} });webview.loadUrl("https://developer.android.com/");
2、Zoom
To enable the built-in zoom內置縮放, set WebSettings.setBuiltInZoomControls(boolean) (introduced in API level CUPCAKE).NOTE: Using zoom if either the height or width is set to WRAP_CONTENT may lead to undefined behavior and should be avoided.注意:如果高度或寬度設置為WRAP_CONTENT,使用縮放可能會導致未定義的行為,應該避免。3、Cookie and window management
For obvious security reasons, your application has its own cache, cookie store etc.—it does not share the Browser application's data.出于明顯的安全原因,您的應用程序具有自己的緩存,cookie存儲等 - 它不共享瀏覽器應用程序的數據。By default, requests by the HTML to open new windows are ignored. This is true whether they be opened by JavaScript or by the target attribute on a link. You can customize your WebChromeClient to provide your own behavior for opening multiple windows, and render them in whatever manner you want.默認情況下,HTML的打開新窗口的請求將被忽略。 無論是通過JavaScript打開還是鏈接上的target屬性,都是如此。 您可以自定義WebChromeClient以提供您自己的打開多個窗口的行為,并以任何您想要的方式呈現它們。
The standard behavior for an Activity is to be destroyed and recreated when the device orientation or any other configuration changes. This will cause the WebView to reload the current page. If you don't want that, you can set your Activity to handle the orientation and keyboardHidden changes, and then just leave the WebView alone. It'll automatically re-orient itself as appropriate. Read Handling Runtime Changes for more information about how to handle configuration changes during runtime.當設備方向或任何其他配置更改時,Activity的標準行為是將被銷毀并重新創建。 這將導致WebView重新加載當前頁面。 如果你不想要這樣做,你可以設置你的Activity來處理方向和鍵盤的隱藏變化,然后就不需要再管WebView了。 它將自動適當的重新定位自己。 有關如何在運行時處理配置更改的更多信息,請閱讀"處理運行時更改"。
4、Building web pages to support different screen densities
The screen density of a device is based on the screen resolution. A screen with low density has fewer available pixels per inch, where a screen with high density has more — sometimes significantly more — pixels per inch. The density of a screen is important because, other things being equal, a UI element (such as a button) whose height and width are defined in terms of screen pixels will appear larger on the lower density screen and smaller on the higher density screen. For simplicity, Android collapses all actual screen densities into three generalized densities: high, medium, and low.設備的屏幕密度基于屏幕分辨率。低密度的屏幕每英寸可用像素更少,其中高密度的屏幕每英寸有更多 - 有時甚至更多的像素。屏幕的密度是重要的,因為其他方面相同時,一個"根據屏幕像素而定義的"UI元素(例如按鈕)的高度和寬度,將在較低密度屏幕上顯示較大,較高密度屏幕上將顯示較小。為了簡單起見,Android將所有實際的屏幕密度折疊成三個廣泛的密度:高,中,低。By default, WebView scales a web page so that it is drawn at a size that matches the default appearance on a medium density screen. So, it applies 1.5x scaling on a high density screen (because its pixels are smaller) and 0.75x scaling on a low density screen (because its pixels are bigger). Starting with API level ECLAIR, WebView supports DOM, CSS, and meta tag features to help you (as a web developer) target screens with different screen densities.默認情況下,WebView會縮放網頁,以使其繪制的尺寸與中密度屏幕上的默認外觀相匹配。因此,它在高密度屏幕上(因為其像素較小)使用1.5倍縮放,在低密度屏幕上(因為其像素較大)使用0.75倍縮放。從API ECLAIR開始,WebView支持DOM,CSS和元標記功能,可幫助您(作為Web開發人員)以不同的屏幕密度來定位屏幕。
Here's a summary of the features you can use to handle different screen densities:以下是可用于處理不同屏幕密度的功能的摘要:
- The window.devicePixelRatio DOM property. The value of this property specifies the default scaling factor used for the current device. For example, if the value of window.devicePixelRatio is "1.0", then the device is considered a medium density (mdpi) device and default scaling is not applied to the web page; if the value is "1.5", then the device is considered a high density device (hdpi) and the page content is scaled 1.5x; if the value is "0.75", then the device is considered a low density device (ldpi) and the content is scaled 0.75x.
window.devicePixelRatio DOM屬性。此屬性的值指定用于當前設備的默認縮放因子。 例如,如果window.devicePixelRatio的值為“1.0”,則該設備被認為是中密度(mdpi)設備,并且默認縮放不應用于網頁;
- The -webkit-device-pixel-ratio CSS media query. Use this to specify the screen densities for which this style sheet is to be used. The corresponding value should be either "0.75", "1", or "1.5", to indicate that the styles are for devices with low density, medium density, or high density screens, respectively. For example:
-webkit-device-pixel-ratio?CSS媒體查詢。使用此來指定"要使用此樣式表的"屏幕密度。相應的值應為“0.75”,“1”或“1.5”,用來表示樣式分別適用于密度低,中等密度或高密度屏幕的設備。 例如:
5、HTML5 Video support
In order to support inline HTML5 video in your application you need to have hardware acceleration turned on.為了在應用程序中支持內置HTML5視頻,您需要啟用硬件加速。6、Full screen support
In order to support full screen — for video or other HTML content — you need to set a WebChromeClient and implement both onShowCustomView(View, WebChromeClient.CustomViewCallback) and onHideCustomView(). If the implementation of either of these two methods is missing then the web contents will not be allowed to enter full screen. Optionally you can implement getVideoLoadingProgressView() to customize the View displayed whilst a video is loading.為了支持全屏幕(視頻或其他HTML內容),您需要設置一個WebChromeClient并實現onShowCustomView和onHideCustomView。 如果這兩種方法中的任何一種的實現丟失,則Web內容將不被允許進入全屏。 或者,您可以實現getVideoLoadingProgressView來自定義視頻加載時顯示的視圖。7、HTML5 Geolocation地理位置 API support
For applications targeting Android N and later releases (API level > M) the geolocation api is only supported on secure origins such as https. For such applications requests to geolocation api on non-secure origins are automatically denied without invoking the corresponding onGeolocationPermissionsShowPrompt(String, GeolocationPermissions.Callback) method.針對Android N和更高版本(API級別> M)的應用程序,地理位置api僅支持安全站點,例如https。 對于這樣的應用程序,對非安全起始地址的api的請求將自動被拒絕,而不會調用相應的onGeolocationPermissionsShowPrompt方法。8、Layout size
It is recommended to set the WebView layout 【height】 to a fixed value or to MATCH_PARENT instead of using WRAP_CONTENT. When using MATCH_PARENT for the height none of the WebView's parents should use a WRAP_CONTENT layout height since that could result in incorrect sizing of the views.建議將WebView布局高度設置為固定值或MATCH_PARENT而不是使用WRAP_CONTENT。當使用MATCH_PARENT高度時,WebView的父布局都不應使用WRAP_CONTENT布局高度,因為這可能會導致視圖的大小不正確。Setting the WebView's 【height】 to WRAP_CONTENT enables the following behaviors:將WebView的高度設置為WRAP_CONTENT將導致以下行為:
- The HTML body layout height is set to a fixed value. This means that elements with a height relative to the HTML body may not be sized correctly.
HTML主體布局高度會被設置為固定值。 這意味著,相對于"HTML主體的"的元素的高度可能不正確。 - For applications targeting KITKAT and earlier SDKs the HTML viewport meta tag will be ignored in order to preserve保持、保留?backwards compatibility.
針對KITKAT和早期SDK的應用程序,HTML視口"元標記"將會被忽略,以保持向后兼容性。
Using a layout 【width】 of WRAP_CONTENT is not supported. If such a width is used the WebView will attempt to use the width of the parent instead.
使用WRAP_CONTENT作為布局寬度是不支持的。 如果使用了這樣的寬度,WebView會嘗試使用父布局的寬度來代替。
9、Metrics?度量
WebView may upload anonymous diagnostic data to Google when the user has consented. This data helps Google improve WebView. Data is collected on a per-app basis for each app which has instantiated a WebView. An individual app can opt out of this feature by putting the following tag in its manifest:當用戶同意后,WebView可能回將匿名診斷數據上傳到Google。 這些數據有助于Google改進WebView。 針對已實例化WebView的每個應用程序,每個應用程序都會收集數據。 單個應用可以通過在其清單中放置以下標簽來選擇停用此功能:<meta-dataandroid:name="android.webkit.WebView.MetricsOptOut"android:value="true"/>Data will only be uploaded for a given app if the user has consented AND the app has not opted out.對于給定的應用,數據將只會在用戶同意并且該應用沒有選擇退出時被上傳。2017-8-17
來自為知筆記(Wiz)
總結
以上是生活随笔為你收集整理的WebView 文档 翻译的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: iptables防火墙火墙服务
- 下一篇: Mybatis中接口和对应的mapper