1. 程式人生 > >jsbridge.BridgeWebView的基礎使用以及屬性詳解

jsbridge.BridgeWebView的基礎使用以及屬性詳解

首先在xml中引入控制元件

  <com.github.lzyzsd.jsbridge.BridgeWebView
            android:id="@+id/xqWebView"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

在activity

bridgeWebView = (BridgeWebView) findViewById(R.id.xqWebView);

        WebSettings websettings = bridgeWebView.getSettings();
        websettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        websettings.setLoadsImagesAutomatically(true);
        websettings.setJavaScriptEnabled(true);
        websettings.setUseWideViewPort(true);
        websettings.setSupportZoom(true);
        websettings.setSupportMultipleWindows(true);
        websettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        websettings.setBuiltInZoomControls(true);
        websettings.setSupportZoom(true);
        websettings.setJavaScriptCanOpenWindowsAutomatically(true);
        websettings.setAppCacheEnabled(true);
        websettings.setDomStorageEnabled(true);

1、setSupportZoom(boolean support)

設定WebView是否支援使用螢幕控制元件或手勢進行縮放,預設是true,支援縮放。

getSettings.setSupportZoom(false);

2、setMediaPlaybackRequiresUserGesture(boolean require)

設定WebView是否通過手勢觸發播放媒體,預設是true,需要手勢觸發。

getSettings.setMediaPlaybackRequiresUserGesture(false);

3、setBuiltInZoomControls(boolean enabled)

設定WebView是否使用其內建的變焦機制,該機制集合螢幕縮放控制元件使用,預設是false,不使用內建變焦機制。

getSettings.setBuiltInZoomControls(true);

4、setDisplayZoomControls(boolean enabled)

設定WebView使用內建縮放機制時,是否展現在螢幕縮放控制元件上,預設true,展現在控制元件上。

getSettings.setDisplayZoomControls(false);

5、setAllowFileAccess(boolean allow)

設定在WebView內部是否允許訪問檔案,預設允許訪問。

getSettings.setAllowFileAccess(false);

6、setAllowContentAccess(boolean allow)

設定WebView是否使用其內建的變焦機制,該機制結合螢幕縮放控制元件使用,預設是false,不使用內建變焦機制。

getSettings.setAllowContentAccess(false);

7、setLoadWithOverviewMode(boolean overview)

設定WebView是否使用預覽模式載入介面。

getSettings.setLoadWithOverviewMode(false);

8、setSaveFormData(boolean save)

設定WebView是否儲存表單資料,預設true,儲存資料。

getSettings.setSaveFormData(false);

9、setTextZoom(int textZoom)

設定WebView中載入頁面字型變焦百分比,預設100,整型數。

getSettings.setTextZoom(100);

10、setAcceptThirdPartyCookies(boolean accept)

設定WebView訪問第三方Cookies策略,參考CookieManager提供的方法:setShouldAcceptThirdPartyCookies。

getSettings.setAcceptThirdPartyCookies(false);

11、setUseWideViewPort(boolean use)

設定WebView是否使用viewport,當該屬性被設定為false時,載入頁面的寬度總是適應WebView控制元件寬度;當被設定為true,當前頁面包含viewport屬性標籤,在標籤中指定寬度值生效,如果頁面不包含viewport標籤,無法提供一個寬度值,這個時候該方法將被使用。

getSettings.setUseWideViewPort(false);

12、setSupportMultipleWindows(boolean support)

設定WebView是否支援多屏視窗,參考WebChromeClient#onCreateWindow,預設false,不支援。

getSettings.setSupportMultipleWindows(true);

13、setLayoutAlgorithm(LayoutAlgorithm l)

設定WebView底層的佈局演算法,參考LayoutAlgorithm#NARROW_COLUMNS,將會重新生成WebView佈局

getSettings.setLayoutAlgorithm(LayoutAlgorithm l);

14、setStandardFontFamily(String font)

設定WebView標準字型庫字型,預設字型“sans-serif”。

getSettings.setStandardFontFamily("sans-serif");

15、setFixedFontFamily(String font)

設定WebView固定的字型庫字型,預設“monospace”。

getSettings.setFixedFontFamily("monospace");

16、setSansSerifFontFamily(String font)

設定WebView Sans SeriFontFamily字型庫字型,預設“sans-serif”。

getSettings.setSansSerifFontFamily("sans-serif");

17、setSerifFontFamily(String font)

設定WebView seri FontFamily字型庫字型,預設“sans-serif”。

getSettings.setSansSerifFontFamily("sans-serif");

18、setCursiveFontFamily(String font)

設定WebView字型庫字型,預設“cursive”

getSettings.setCursiveFontFamily("cursive");

19、setFantasyFontFamily(String font)

設定WebView字型庫字型,預設“fantasy”。

getSettings.setFantasyFontFamily("fantasy");

20、setMinimumFontSize(int size)

設定WebView字型最小值,預設值8,取值1到72

getSettings.setMinimumFontSize(8);

21、setMinimumLogicalFontSize(int size)

設定WebView邏輯上最小字型值,預設值8,取值1到72

getSettings.setMinimumLogicalFontSize(8);

22、setDefaultFontSize(int size)

設定WebView預設值字型值,預設值16,取值1到72

getSettings.setDefaultFontSize(16);

23、setDefaultFixedFontSize(int size)

設定WebView預設固定的字型值,預設值16,取值1到72

getSettings.setDefaultFixedFontSize(16);

24、setLoadsImagesAutomatically(boolean flag)

設定WebView是否載入圖片資源,預設true,自動載入圖片

getSettings.setLoadsImagesAutomatically(false);

25、setBlockNetworkImage(boolean flag)

設定WebView是否以http、https方式訪問從網路載入圖片資源,預設false

getSettings.setBlockNetworkImage(true);

26、setBlockNetworkLoads(boolean flag)

設定WebView是否從網路載入資源,Application需要設定訪問網路許可權,否則報異常

getSettings.setBlockNetworkLoads(true);

27、setJavaScriptEnabled(boolean flag)

設定WebView是否允許執行JavaScript指令碼,預設false,不允許

getSettings.setJavaScriptEnabled(true);

28、setAllowUniversalAccessFromFileURLs(boolean flag)

設定WebView執行中的指令碼可以是否訪問任何原始起點內容,預設true

getSettings.setAllowUniversalAccessFromFileURLs(false);

29、setAllowFileAccessFromFileURLs(boolean flag)

設定WebView執行中的一個檔案方案被允許訪問其他檔案方案中的內容,預設值true

getSettings.setAllowFileAccessFromFileURLs(false);

30、setGeolocationDatabasePath(String databasePath)

設定WebView儲存地理位置資訊資料路徑,指定的路徑Application具備寫入許可權

getSettings.setGeolocationDatabasePath(String path);

31、setAppCacheEnabled(boolean flag)

設定Application快取API是否開啟,預設false,設定有效的快取路徑參考setAppCachePath(String path)方法

getSettings.setAppCacheEnabled(true);

32、setAppCachePath(String appCachePath)

設定當前Application快取檔案路徑,Application Cache API能夠開啟需要指定Application具備寫入許可權的路徑

getSettings.setAppCachePath(String appCachePath);

33、setDatabaseEnabled(boolean flag)

設定是否開啟資料庫儲存API許可權,預設false,未開啟,可以參考setDatabasePath(String path)

getSettings.setDatabaseEnabled(false);

34、setDomStorageEnabled(boolean flag)

設定是否開啟DOM儲存API許可權,預設false,未開啟,設定為true,WebView能夠使用DOM storage API

getSettings.setDomStorageEnabled(true);

35、setGeolocationEnabled(boolean flag)

設定是否開啟定位功能,預設true,開啟定位

getSettings.setGeolocationEnabled(false);

36、setJavaScriptCanOpenWindowsAutomatically(boolean flag)

設定指令碼是否允許自動開啟彈窗,預設false,不允許

getSettings.setJavaScriptCanOpenWindowsAutomatically(true);

37、setDefaultTextEncodingName(String encoding)

設定WebView載入頁面文字內容的編碼,預設“UTF-8”。

getSettings.setDefaultTextEncodingName("UTF-8");

38、setUserAgentString(String ua)

設定WebView代理字串,如果String為null或為空,將使用系統預設值

getSettings.setUserAgentString(String ua);

39、setNeedInitialFocus(boolean flag)

設定WebView是否需要設定一個節點獲取焦點當被回撥的時候,預設true

getSettings.setNeedInitialFocus(false);

40、setCacheMode(int mode)

重寫快取被使用到的方法,該方法基於Navigation Type,載入普通的頁面,將會檢查快取同時重新驗證是否需要載入,如果不需要重新載入,將直接從快取讀取資料,允許客戶端通過指定LOAD_DEFAULT、LOAD_CACHE_ELSE_NETWORK、LOAD_NO_CACHE、LOAD_CACHE_ONLY其中之一重寫該行為方法,預設值LOAD_DEFAULT

getSettings.setCacheMode(WebSettings.LOAD_DEFAULT);

41、setMixedContentMode(int mode)

設定當一個安全站點企圖載入來自一個不安全站點資源時WebView的行為,android.os.Build.VERSION_CODES.KITKAT預設為MIXED_CONTENT_ALWAYS_ALLOW,android.os.Build.VERSION_CODES#LOLLIPOP預設為MIXED_CONTENT_NEVER_ALLOW,取值其中之一:MIXED_CONTENT_NEVER_ALLOW、MIXED_CONTENT_ALWAYS_ALLOW、MIXED_CONTENT_COMPATIBILITY_MODE.

getSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);

出處:https://www.jianshu.com/p/0d7d429bd216

 若從h5接受訊息:


        bridgeWebView.registerHandler("loginAction", new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {
                if (data.equals("hello")) {
                    Intent intent = new Intent(ActivityCarXQ.this, ActivityCarXqJgt.class);
                    startActivity(intent);
                } else {
                    Toast.makeText(ActivityCarXQ.this, "未從網路獲取列印資料!", Toast.LENGTH_LONG).show();
                }
            }
        });

傳送訊息給h5:

   final JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("c_oem_brand", title);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        bridgeWebView.callHandler("registerAction", jsonObject.toString(), new CallBackFunction() {
            @Override
            public void onCallBack(String data) {

            }
        });

 

其中第一個欄位需和h5約定一致