WebView控件之WebSettings詳解

WebSettings用于管理WebView狀態(tài)配置,當(dāng)WebView第一次被創(chuàng)建時(shí),WebView包含著一個(gè)默認(rèn)的配置,這些默認(rèn)的配置將通過(guò)get方法返回,通過(guò)WebView中的getSettings方法獲得一個(gè)WebSettings對(duì)象,如果一個(gè)WebView被銷(xiāo)毀,在WebSettings中所有回調(diào)方法將拋出IllegalStateException異常。

1、setSupportZoom(boolean support)

設(shè)置WebView是否支持使用屏幕控件或手勢(shì)進(jìn)行縮放,默認(rèn)是true,支持縮放。

getSettings.setSupportZoom(false);

2、setMediaPlaybackRequiresUserGesture(boolean require)

設(shè)置WebView是否通過(guò)手勢(shì)觸發(fā)播放媒體,默認(rèn)是true,需要手勢(shì)觸發(fā)。

getSettings.setMediaPlaybackRequiresUserGesture(false);

3、setBuiltInZoomControls(boolean enabled)

設(shè)置WebView是否使用其內(nèi)置的變焦機(jī)制,該機(jī)制集合屏幕縮放控件使用,默認(rèn)是false,不使用內(nèi)置變焦機(jī)制。

getSettings.setBuiltInZoomControls(true);

4、setDisplayZoomControls(boolean enabled)

設(shè)置WebView使用內(nèi)置縮放機(jī)制時(shí),是否展現(xiàn)在屏幕縮放控件上,默認(rèn)true,展現(xiàn)在控件上。

getSettings.setDisplayZoomControls(false);

5、setAllowFileAccess(boolean allow)

設(shè)置在WebView內(nèi)部是否允許訪問(wèn)文件,默認(rèn)允許訪問(wèn)。

getSettings.setAllowFileAccess(false);

6、setAllowContentAccess(boolean allow)

設(shè)置WebView是否使用其內(nèi)置的變焦機(jī)制,該機(jī)制結(jié)合屏幕縮放控件使用,默認(rèn)是false,不使用內(nèi)置變焦機(jī)制。

getSettings.setAllowContentAccess(false);

7、setLoadWithOverviewMode(boolean overview)

設(shè)置WebView是否使用預(yù)覽模式加載界面。

getSettings.setLoadWithOverviewMode(false);

8、setSaveFormData(boolean save)

設(shè)置WebView是否保存表單數(shù)據(jù),默認(rèn)true,保存數(shù)據(jù)。

getSettings.setSaveFormData(false);

9、setTextZoom(int textZoom)

設(shè)置WebView中加載頁(yè)面字體變焦百分比,默認(rèn)100,整型數(shù)。

getSettings.setTextZoom(100);

10、setAcceptThirdPartyCookies(boolean accept)

設(shè)置WebView訪問(wèn)第三方Cookies策略,參考CookieManager提供的方法:setShouldAcceptThirdPartyCookies。

getSettings.setAcceptThirdPartyCookies(false);

11、setUseWideViewPort(boolean use)

設(shè)置WebView是否使用viewport,當(dāng)該屬性被設(shè)置為false時(shí),加載頁(yè)面的寬度總是適應(yīng)WebView控件寬度;當(dāng)被設(shè)置為true,當(dāng)前頁(yè)面包含viewport屬性標(biāo)簽,在標(biāo)簽中指定寬度值生效,如果頁(yè)面不包含viewport標(biāo)簽,無(wú)法提供一個(gè)寬度值,這個(gè)時(shí)候該方法將被使用。

getSettings.setUseWideViewPort(false);

12、setSupportMultipleWindows(boolean support)

設(shè)置WebView是否支持多屏窗口,參考WebChromeClient#onCreateWindow,默認(rèn)false,不支持。

getSettings.setSupportMultipleWindows(true);

13、setLayoutAlgorithm(LayoutAlgorithm l)

設(shè)置WebView底層的布局算法,參考LayoutAlgorithm#NARROW_COLUMNS,將會(huì)重新生成WebView布局

getSettings.setLayoutAlgorithm(LayoutAlgorithm l);

14、setStandardFontFamily(String font)

設(shè)置WebView標(biāo)準(zhǔn)字體庫(kù)字體,默認(rèn)字體“sans-serif”。

getSettings.setStandardFontFamily("sans-serif");

15、setFixedFontFamily(String font)

設(shè)置WebView固定的字體庫(kù)字體,默認(rèn)“monospace”。

getSettings.setFixedFontFamily("monospace");

16、setSansSerifFontFamily(String font)

設(shè)置WebView Sans SeriFontFamily字體庫(kù)字體,默認(rèn)“sans-serif”。

getSettings.setSansSerifFontFamily("sans-serif");

17、setSerifFontFamily(String font)

設(shè)置WebView seri FontFamily字體庫(kù)字體,默認(rèn)“sans-serif”。

getSettings.setSansSerifFontFamily("sans-serif");

18、setCursiveFontFamily(String font)

設(shè)置WebView字體庫(kù)字體,默認(rèn)“cursive”

getSettings.setCursiveFontFamily("cursive");

19、setFantasyFontFamily(String font)

設(shè)置WebView字體庫(kù)字體,默認(rèn)“fantasy”。

getSettings.setFantasyFontFamily("fantasy");

20、setMinimumFontSize(int size)

設(shè)置WebView字體最小值,默認(rèn)值8,取值1到72

getSettings.setMinimumFontSize(8);

21、setMinimumLogicalFontSize(int size)

設(shè)置WebView邏輯上最小字體值,默認(rèn)值8,取值1到72

getSettings.setMinimumLogicalFontSize(8);

22、setDefaultFontSize(int size)

設(shè)置WebView默認(rèn)值字體值,默認(rèn)值16,取值1到72

getSettings.setDefaultFontSize(16);

23、setDefaultFixedFontSize(int size)

設(shè)置WebView默認(rèn)固定的字體值,默認(rèn)值16,取值1到72

getSettings.setDefaultFixedFontSize(16);

24、setLoadsImagesAutomatically(boolean flag)

設(shè)置WebView是否加載圖片資源,默認(rèn)true,自動(dòng)加載圖片

getSettings.setLoadsImagesAutomatically(false);

25、setBlockNetworkImage(boolean flag)

設(shè)置WebView是否以http、https方式訪問(wèn)從網(wǎng)絡(luò)加載圖片資源,默認(rèn)false

getSettings.setBlockNetworkImage(true);

26、setBlockNetworkLoads(boolean flag)

設(shè)置WebView是否從網(wǎng)絡(luò)加載資源,Application需要設(shè)置訪問(wèn)網(wǎng)絡(luò)權(quán)限,否則報(bào)異常

getSettings.setBlockNetworkLoads(true);

27、setJavaScriptEnabled(boolean flag)

設(shè)置WebView是否允許執(zhí)行JavaScript腳本,默認(rèn)false,不允許

getSettings.setJavaScriptEnabled(true);

28、setAllowUniversalAccessFromFileURLs(boolean flag)

設(shè)置WebView運(yùn)行中的腳本可以是否訪問(wèn)任何原始起點(diǎn)內(nèi)容,默認(rèn)true

getSettings.setAllowUniversalAccessFromFileURLs(false);

29、setAllowFileAccessFromFileURLs(boolean flag)

設(shè)置WebView運(yùn)行中的一個(gè)文件方案被允許訪問(wèn)其他文件方案中的內(nèi)容,默認(rèn)值true

getSettings.setAllowFileAccessFromFileURLs(false);

30、setGeolocationDatabasePath(String databasePath)

設(shè)置WebView保存地理位置信息數(shù)據(jù)路徑,指定的路徑Application具備寫(xiě)入權(quán)限

getSettings.setGeolocationDatabasePath(String path);

31、setAppCacheEnabled(boolean flag)

設(shè)置Application緩存API是否開(kāi)啟,默認(rèn)false,設(shè)置有效的緩存路徑參考setAppCachePath(String path)方法

getSettings.setAppCacheEnabled(true);

32、setAppCachePath(String appCachePath)

設(shè)置當(dāng)前Application緩存文件路徑,Application Cache API能夠開(kāi)啟需要指定Application具備寫(xiě)入權(quán)限的路徑

getSettings.setAppCachePath(String appCachePath);

33、setDatabaseEnabled(boolean flag)

設(shè)置是否開(kāi)啟數(shù)據(jù)庫(kù)存儲(chǔ)API權(quán)限,默認(rèn)false,未開(kāi)啟,可以參考setDatabasePath(String path)

getSettings.setDatabaseEnabled(false);

34、setDomStorageEnabled(boolean flag)

設(shè)置是否開(kāi)啟DOM存儲(chǔ)API權(quán)限,默認(rèn)false,未開(kāi)啟,設(shè)置為true,WebView能夠使用DOM storage API

getSettings.setDomStorageEnabled(true);

35、setGeolocationEnabled(boolean flag)

設(shè)置是否開(kāi)啟定位功能,默認(rèn)true,開(kāi)啟定位

getSettings.setGeolocationEnabled(false);

36、setJavaScriptCanOpenWindowsAutomatically(boolean flag)

設(shè)置腳本是否允許自動(dòng)打開(kāi)彈窗,默認(rèn)false,不允許

getSettings.setJavaScriptCanOpenWindowsAutomatically(true);

37、setDefaultTextEncodingName(String encoding)

設(shè)置WebView加載頁(yè)面文本內(nèi)容的編碼,默認(rèn)“UTF-8”。

getSettings.setDefaultTextEncodingName("UTF-8");

38、setUserAgentString(String ua)

設(shè)置WebView代理字符串,如果String為null或?yàn)榭眨瑢⑹褂孟到y(tǒng)默認(rèn)值

getSettings.setUserAgentString(String ua);

39、setNeedInitialFocus(boolean flag)

設(shè)置WebView是否需要設(shè)置一個(gè)節(jié)點(diǎn)獲取焦點(diǎn)當(dāng)被回調(diào)的時(shí)候,默認(rèn)true

getSettings.setNeedInitialFocus(false);

40、setCacheMode(int mode)

重寫(xiě)緩存被使用到的方法,該方法基于Navigation Type,加載普通的頁(yè)面,將會(huì)檢查緩存同時(shí)重新驗(yàn)證是否需要加載,如果不需要重新加載,將直接從緩存讀取數(shù)據(jù),允許客戶端通過(guò)指定LOAD_DEFAULT、LOAD_CACHE_ELSE_NETWORK、LOAD_NO_CACHE、LOAD_CACHE_ONLY其中之一重寫(xiě)該行為方法,默認(rèn)值LOAD_DEFAULT

getSettings.setCacheMode(WebSettings.LOAD_DEFAULT);

41、setMixedContentMode(int mode)

設(shè)置當(dāng)一個(gè)安全站點(diǎn)企圖加載來(lái)自一個(gè)不安全站點(diǎn)資源時(shí)WebView的行為,android.os.Build.VERSION_CODES.KITKAT默認(rèn)為MIXED_CONTENT_ALWAYS_ALLOW,android.os.Build.VERSION_CODES#LOLLIPOP默認(rèn)為MIXED_CONTENT_NEVER_ALLOW,取值其中之一:MIXED_CONTENT_NEVER_ALLOW、MIXED_CONTENT_ALWAYS_ALLOW、MIXED_CONTENT_COMPATIBILITY_MODE.

getSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容