Java Code Examples for android.webkit.WebSettings#setTextZoom()

The following examples show how to use android.webkit.WebSettings#setTextZoom() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ExtendedWebView.java    From ForPDA with GNU General Public License v3.0 6 votes vote down vote up
@SuppressLint("SetJavaScriptEnabled")
public void init() {
    mUiThread = Thread.currentThread();
    audioManager = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE);
    addJavascriptInterface(this, IBase.JS_BASE_INTERFACE);
    WebSettings settings = getSettings();
    settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
    settings.setBuiltInZoomControls(false);
    settings.setDefaultFontSize(16);
    settings.setTextZoom(100);
    settings.setJavaScriptEnabled(true);
    settings.setAllowFileAccess(true);
    settings.setAllowContentAccess(true);
    settings.setAllowFileAccessFromFileURLs(true);
    settings.setAllowUniversalAccessFromFileURLs(true);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
    }
    setRelativeFontSize(Preferences.Main.getWebViewSize(getContext()));
    setBackgroundColor(App.getColorFromAttr(getContext(), R.attr.background_base));
}
 
Example 2
Source File: WebConfigImpl.java    From PoupoLayer with MIT License 5 votes vote down vote up
/**
 * 默认webview设置
 * @param settings
 */
private void initSetting(WebSettings settings, Context context){
    //5.0以上开启混合模式加载
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
    }
    settings.setLoadWithOverviewMode(true);
    settings.setUseWideViewPort(true);
    //允许js代码 在Android 4.3版本调用WebSettings.setJavaScriptEnabled()方法时会调用一下reload方法,同时会回调多次WebChromeClient.onJsPrompt()
    settings.setJavaScriptEnabled(true);
    //允许SessionStorage/LocalStorage存储
    settings.setDomStorageEnabled(true);
    //禁用放缩
    settings.setDisplayZoomControls(false);
    settings.setBuiltInZoomControls(false);
    //禁用文字缩放
    settings.setTextZoom(100);
    //10M缓存,api 18后,系统自动管理。
    settings.setAppCacheMaxSize(10 * 1024 * 1024);
    //允许缓存,设置缓存位置 缓存位置由用户指定
    settings.setAppCacheEnabled(true);
    settings.setAppCachePath(context.getDir("appcache", 0).getPath());
    //允许WebView使用File协议
    settings.setAllowFileAccess(true);
    //不保存密码
    settings.setSavePassword(false);
    //自动加载图片
    settings.setLoadsImagesAutomatically(true);
}
 
Example 3
Source File: MessagesActivity.java    From SlimSocial-for-Facebook with GNU General Public License v2.0 5 votes vote down vote up
private void SetupMessagesWebView() {
    webViewMessages = findViewById(R.id.webViewMessages);
    webViewMessages.setListener(this, this);
    webViewMessages.addPermittedHostname("mbasic.facebook.com");

    webViewMessages.setDesktopMode(false);

    webViewMessages.requestFocus(View.FOCUS_DOWN);
    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
    //remove the keyboard issue


    WebSettings settings = webViewMessages.getSettings();
    //set text zoom
    int zoom = Integer.parseInt(savedPreferences.getString("pref_textSize", "100"));
    settings.setTextZoom(zoom);


    // Use WideViewport and Zoom out if there is no viewport defined
    settings.setUseWideViewPort(false);
    settings.setLoadWithOverviewMode(false);


    // better image sizing support
    settings.setSupportZoom(false);
    settings.setDisplayZoomControls(false);
    settings.setBuiltInZoomControls(false);

    // Hide the zoom controls for HONEYCOMB+
    settings.setDisplayZoomControls(false);
}
 
Example 4
Source File: ArticleWebView.java    From aard2-android with GNU General Public License v3.0 5 votes vote down vote up
boolean textZoomOut() {
    WebSettings settings = getSettings();
    int newZoom = settings.getTextZoom() - 20;
    if (newZoom >= 40) {
        settings.setTextZoom(newZoom);
        saveTextZoomPref();
        return true;
    }
    else {
        return false;
    }
}
 
Example 5
Source File: ReadabilityActivity.java    From Ninja with Apache License 2.0 5 votes vote down vote up
private void initWebView() {
    webView.setAlwaysDrawnWithCacheEnabled(true);
    webView.setAnimationCacheEnabled(true);
    webView.setDrawingCacheBackgroundColor(0x00000000);
    webView.setDrawingCacheEnabled(true);
    webView.setWillNotCacheDrawing(false);
    webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);

    webView.setFocusable(true);
    webView.setFocusableInTouchMode(true);
    webView.setScrollbarFadingEnabled(true);
    webView.setHorizontalScrollBarEnabled(true);
    webView.setVerticalScrollBarEnabled(true);

    webView.setBackground(null);
    webView.getRootView().setBackground(null);
    int color = sp.getInt(getString(R.string.sp_readability_background), getResources().getColor(R.color.white));
    webView.setBackgroundColor(color);

    WebSettings webSettings = webView.getSettings();
    webSettings.setAppCacheEnabled(true);
    webSettings.setAppCachePath(getCacheDir().toString());
    webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
    webSettings.setDefaultTextEncodingName(BrowserUnit.URL_ENCODING);
    webSettings.setSupportZoom(true);
    webSettings.setBuiltInZoomControls(true);
    webSettings.setDisplayZoomControls(false);
    webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
    webSettings.setLoadWithOverviewMode(true);
    webSettings.setTextZoom(100);
    webSettings.setUseWideViewPort(true);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        webSettings.setLoadsImagesAutomatically(true);
    } else {
        webSettings.setLoadsImagesAutomatically(false);
    }
}
 
Example 6
Source File: ArticleWebView.java    From aard2-android with GNU General Public License v3.0 5 votes vote down vote up
boolean textZoomIn() {
    WebSettings settings = getSettings();
    int newZoom = settings.getTextZoom() + 20;
    if (newZoom <= 200) {
        settings.setTextZoom(newZoom);
        saveTextZoomPref();
        return true;
    }
    else {
        return false;
    }
}
 
Example 7
Source File: TopSlidWebView.java    From MyBlogDemo with Apache License 2.0 5 votes vote down vote up
@SuppressLint({"AddJavascriptInterface", "SetJavaScriptEnabled"})
    private void init(Context context) {
        post(new Runnable() {
            @Override
            public void run() {
                mWebViewLayout = (WebViewLayout) getParent();
//                topMsgView = mWebViewLayout.getTopMsgView();
            }
        });
        connectionManager = (ConnectivityManager)
                context.getSystemService(Context.CONNECTIVITY_SERVICE);


        WebSettings settings = getSettings();
        settings.setUseWideViewPort(true);
        settings.setLoadWithOverviewMode(true);
        settings.setDisplayZoomControls(true);
        settings.setJavaScriptEnabled(true);
        settings.setTextZoom(200);
//        initSize(settings);
        addJavascriptInterface(new CallBack() {
            @JavascriptInterface
            public void goBack() {
//                reload();
                mHandler.sendEmptyMessage(RELOAD);
                Toast.makeText(getContext(), "call back", Toast.LENGTH_SHORT).show();
            }
        }, "android");
//        setWebViewClient(webViewClient);
//        setWebChromeClient(webChromeClient);

    }
 
Example 8
Source File: ByWebView.java    From ByWebView with Apache License 2.0 5 votes vote down vote up
@SuppressLint("SetJavaScriptEnabled")
    private void handleSetting() {
        WebSettings ws = mWebView.getSettings();
        // 保存表单数据
        ws.setSaveFormData(true);
        // 是否应该支持使用其屏幕缩放控件和手势缩放
        ws.setSupportZoom(true);
        ws.setBuiltInZoomControls(true);
        ws.setDisplayZoomControls(false);
        // 启动应用缓存
        ws.setAppCacheEnabled(true);
        // 设置缓存模式
        ws.setCacheMode(WebSettings.LOAD_DEFAULT);
        // setDefaultZoom  api19被弃用
        // 网页内容的宽度自适应屏幕
        ws.setLoadWithOverviewMode(true);
        ws.setUseWideViewPort(true);
        // 告诉WebView启用JavaScript执行。默认的是false。
        ws.setJavaScriptEnabled(true);
        //  页面加载好以后,再放开图片
        ws.setBlockNetworkImage(false);
        // 使用localStorage则必须打开
        ws.setDomStorageEnabled(true);
        // 排版适应屏幕
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            ws.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        } else {
            ws.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
        }
        // WebView是否新窗口打开(加了后可能打不开网页)
//        ws.setSupportMultipleWindows(true);

        // webview从5.0开始默认不允许混合模式,https中不能加载http资源,需要设置开启。
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            ws.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        /** 设置字体默认缩放大小(改变网页字体大小,setTextSize  api14被弃用)*/
        ws.setTextZoom(100);
    }
 
Example 9
Source File: ArticleWebView.java    From aard2-android with GNU General Public License v3.0 4 votes vote down vote up
void applyTextZoomPref() {
    SharedPreferences prefs = prefs();
    int textZoom = prefs.getInt(PREF_TEXT_ZOOM, 100);
    WebSettings settings = getSettings();
    settings.setTextZoom(textZoom);
}
 
Example 10
Source File: MainActivity.java    From SlimSocial-for-Facebook with GNU General Public License v2.0 4 votes vote down vote up
private void SetupWebView() {
        webViewFacebook = findViewById(webView);
        webViewFacebook.setListener(this, this);

        webViewFacebook.clearPermittedHostnames();
        webViewFacebook.addPermittedHostname("facebook.com");
        webViewFacebook.addPermittedHostname("fbcdn.net");
        webViewFacebook.addPermittedHostname("fb.com");
        webViewFacebook.addPermittedHostname("fb.me");

/*
        webViewFacebook.addPermittedHostname("m.facebook.com");
        webViewFacebook.addPermittedHostname("h.facebook.com");
        webViewFacebook.addPermittedHostname("touch.facebook.com");
        webViewFacebook.addPermittedHostname("mbasic.facebook.com");
        webViewFacebook.addPermittedHostname("touch.facebook.com");
        webViewFacebook.addPermittedHostname("messenger.com");
*/

        webViewFacebook.requestFocus(View.FOCUS_DOWN);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);//remove the keyboard issue

        WebSettings settings = webViewFacebook.getSettings();

        webViewFacebook.setDesktopMode(true);
        settings.setUserAgentString("Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36");
        settings.setJavaScriptEnabled(true);

        //set text zoom
        int zoom = Integer.parseInt(savedPreferences.getString("pref_textSize", "100"));
        settings.setTextZoom(zoom);

        //set Geolocation
        settings.setGeolocationEnabled(savedPreferences.getBoolean("pref_allowGeolocation", true));

        // Use WideViewport and Zoom out if there is no viewport defined
        settings.setUseWideViewPort(true);
        settings.setLoadWithOverviewMode(true);

        // better image sizing support
        settings.setSupportZoom(true);
        settings.setDisplayZoomControls(false);
        settings.setBuiltInZoomControls(true);

        // set caching
        settings.setAppCachePath(getCacheDir().getAbsolutePath());
        settings.setAppCacheEnabled(true);

        settings.setLoadsImagesAutomatically(!savedPreferences.getBoolean("pref_doNotDownloadImages", false));//to save data

        settings.setDisplayZoomControls(false);
    }
 
Example 11
Source File: WebViewActivity.java    From NetEasyNews with GNU General Public License v3.0 4 votes vote down vote up
private void initWebView() {
    mProgressBar.setVisibility(View.VISIBLE);
    WebSettings ws = webView.getSettings();
    // 网页内容的宽度是否可大于WebView控件的宽度
    ws.setLoadWithOverviewMode(false);
    // 保存表单数据
    ws.setSaveFormData(true);
    // 是否应该支持使用其屏幕缩放控件和手势缩放
    ws.setSupportZoom(true);
    ws.setBuiltInZoomControls(true);
    ws.setDisplayZoomControls(false);
    // 启动应用缓存
    ws.setAppCacheEnabled(true);
    // 设置缓存模式
    ws.setCacheMode(WebSettings.LOAD_DEFAULT);
    // setDefaultZoom  api19被弃用
    // 设置此属性,可任意比例缩放。
    ws.setUseWideViewPort(true);
    // 缩放比例 1
    webView.setInitialScale(1);
    // 告诉WebView启用JavaScript执行。默认的是false。
    ws.setJavaScriptEnabled(true);
    //  页面加载好以后,再放开图片
    ws.setBlockNetworkImage(false);
    // 使用localStorage则必须打开
    ws.setDomStorageEnabled(true);
    // 排版适应屏幕
    ws.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
    // WebView是否支持多个窗口。
    ws.setSupportMultipleWindows(true);

    // webview从5.0开始默认不允许混合模式,https中不能加载http资源,需要设置开启。
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        ws.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
    }
    /** 设置字体默认缩放大小(改变网页字体大小,setTextSize  api14被弃用)*/
    ws.setTextZoom(100);

    mWebChromeClient = new MyWebChromeClient(this);
    webView.setWebChromeClient(mWebChromeClient);
    // 与js交互
    webView.addJavascriptInterface(new ImageClickInterface(this), "injectedObject");
    webView.setWebViewClient(new MyWebViewClient(this));
}
 
Example 12
Source File: WebViewActivity.java    From CloudReader with Apache License 2.0 4 votes vote down vote up
@SuppressLint("SetJavaScriptEnabled")
    private void initWebView() {
        WebSettings ws = webView.getSettings();
        // 网页内容的宽度适配
        ws.setLoadWithOverviewMode(true);
        ws.setUseWideViewPort(true);
        // 保存表单数据
        ws.setSaveFormData(true);
        // 是否应该支持使用其屏幕缩放控件和手势缩放
        ws.setSupportZoom(true);
        ws.setBuiltInZoomControls(true);
        ws.setDisplayZoomControls(false);
        // 启动应用缓存
        ws.setAppCacheEnabled(true);
        // 设置缓存模式
        ws.setCacheMode(WebSettings.LOAD_DEFAULT);
        // 告诉WebView启用JavaScript执行。默认的是false。
        ws.setJavaScriptEnabled(true);
        //  页面加载好以后,再放开图片
        ws.setBlockNetworkImage(false);
        // 使用localStorage则必须打开
        ws.setDomStorageEnabled(true);
        // 排版适应屏幕
        ws.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        // WebView是否新窗口打开(加了后可能打不开网页)
//        ws.setSupportMultipleWindows(true);

        // webview从5.0开始默认不允许混合模式,https中不能加载http资源,需要设置开启。
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            ws.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        /** 设置字体默认缩放大小(改变网页字体大小,setTextSize  api14被弃用)*/
        ws.setTextZoom(100);

        mWebChromeClient = new MyWebChromeClient(this);
        webView.setWebChromeClient(mWebChromeClient);
        // 与js交互
        webView.addJavascriptInterface(new ImageClickInterface(this), "injectedObject");
        webView.setWebViewClient(new MyWebViewClient(this));
        webView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                return handleLongImage();
            }
        });
    }
 
Example 13
Source File: WebViewActivity.java    From ByWebView with Apache License 2.0 4 votes vote down vote up
@SuppressLint({"SetJavaScriptEnabled", "AddJavascriptInterface"})
    private void initWebView() {
        WebSettings ws = webView.getSettings();
        // 保存表单数据
        ws.setSaveFormData(true);
        // 是否应该支持使用其屏幕缩放控件和手势缩放
        ws.setSupportZoom(true);
        ws.setBuiltInZoomControls(true);
        ws.setDisplayZoomControls(false);
        // 启动应用缓存
        ws.setAppCacheEnabled(true);
        // 设置缓存模式
        ws.setCacheMode(WebSettings.LOAD_DEFAULT);
        // setDefaultZoom  api19被弃用
        // 网页内容的宽度自适应屏幕
        ws.setLoadWithOverviewMode(true);
        ws.setUseWideViewPort(true);
        // 网页缩放至100,一般的网页达到屏幕宽度效果,个别除外
//        webView.setInitialScale(100);
        // 关掉下滑弧形阴影
//        webView.setOverScrollMode(WebView.OVER_SCROLL_NEVER);
        // 告诉WebView启用JavaScript执行。默认的是false。
        ws.setJavaScriptEnabled(true);
        //  页面加载好以后,再放开图片
        ws.setBlockNetworkImage(false);
        // 使用localStorage则必须打开
        ws.setDomStorageEnabled(true);
        // 排版适应屏幕
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            ws.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        } else {
            ws.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
        }
        // WebView是否新窗口打开(加了后可能打不开网页)
//        ws.setSupportMultipleWindows(true);

        // webview从5.0开始默认不允许混合模式,https中不能加载http资源,需要设置开启。
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            ws.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        /** 设置字体默认缩放大小(改变网页字体大小,setTextSize  api14被弃用)*/
        ws.setTextZoom(100);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            webView.setScrollBarSize(WebTools.dp2px(this, 3));
        }

        mWebChromeClient = new MyWebChromeClient(this);
        webView.setWebChromeClient(mWebChromeClient);
        // 与js交互
        webView.addJavascriptInterface(new MyJavascriptInterface(this), "injectedObject");
        webView.setWebViewClient(new MyWebViewClient(this));
        webView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                return handleLongImage();
            }
        });

    }
 
Example 14
Source File: NestedWebview.java    From SimplicityBrowser with MIT License 4 votes vote down vote up
@SuppressLint({"SetJavaScriptEnabled"})
public void initializeSettings(){
    boolean isTablet = getResources().getBoolean(R.bool.isTablet);
    String lang = Locale.getDefault().getDisplayLanguage();
    CookieManager cookieManager = CookieManager.getInstance();
    cookieManager.setAcceptCookie(true);
    cookieManager.setAcceptThirdPartyCookies(NestedWebview.this, true);
    WebSettings mWebSettings = getSettings();
    mWebSettings.setJavaScriptEnabled(true);
    mWebSettings.setJavaScriptCanOpenWindowsAutomatically(true);
    mWebSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);
    mWebSettings.setBuiltInZoomControls(true);
    mWebSettings.setDisplayZoomControls(false);
    mWebSettings.setMediaPlaybackRequiresUserGesture(false);
    if(isTablet){
        mWebSettings.setUserAgentString("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Simplicity/57.0.3098.116");
        mWebSettings.setLoadWithOverviewMode(true);
        mWebSettings.setUseWideViewPort(true);
    }else{
        mWebSettings.setUserAgentString(null);
        mWebSettings.setLoadWithOverviewMode(true);
        mWebSettings.setUseWideViewPort(true);
    }
    mWebSettings.setAppCacheEnabled(true);
    mWebSettings.setDatabaseEnabled(true);
    if (UserPreferences.getBoolean("enable_location", false)) {
        mWebSettings.setGeolocationEnabled(true);
    } else {
        mWebSettings.setGeolocationEnabled(false);
    }
    if(UserPreferences.getBoolean("lite_mode", false)){
        mWebSettings.setLoadsImagesAutomatically(false);
    }else{
        mWebSettings.setLoadsImagesAutomatically(true);
    }
    mWebSettings.setAllowFileAccessFromFileURLs(true);
    mWebSettings.setAllowUniversalAccessFromFileURLs(true);
    mWebSettings.setDomStorageEnabled(true);
    mWebSettings.setTextZoom(Integer.parseInt(UserPreferences.getInstance(WEB_ACTIVITY).getFont()));
    addJavascriptInterface(new ReaderHandler(SimplicityApplication.getContextOfApplication(), MainActivity.getMainActivity()), "simplicity_reader");
    if(UserPreferences.getBoolean("facebook_photos", false)){
        addJavascriptInterface(new ImageInterface(WEB_ACTIVITY), "Photos");
    }

}
 
Example 15
Source File: BookPageFragment.java    From IslamicLibraryAndroid with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void setZoom(int newZoom) {
    WebSettings webSettings = mBookPageWebView.getSettings();
    if (newZoom != webSettings.getTextZoom())
        webSettings.setTextZoom(newZoom);
}
 
Example 16
Source File: WebViewPresenter.java    From qvod with MIT License 4 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void setUpWebViewDefaults(WebView webView) {
    WebSettings settings = webView.getSettings();

    // 网页内容的宽度是否可大于WebView控件的宽度
    settings.setLoadWithOverviewMode(false);
    // 保存表单数据
    settings.setSaveFormData(true);
    // 是否应该支持使用其屏幕缩放控件和手势缩放
    settings.setSupportZoom(true);
    settings.setBuiltInZoomControls(true);
    settings.setDisplayZoomControls(false);
    // 启动应用缓存
    settings.setAppCacheEnabled(true);
    // 设置缓存模式
    settings.setCacheMode(WebSettings.LOAD_DEFAULT);
    // setDefaultZoom  api19被弃用
    // 设置此属性,可任意比例缩放。
    settings.setUseWideViewPort(true);
    // 缩放比例 1
    webView.setInitialScale(1);
    // 告诉WebView启用JavaScript执行。默认的是false。
    settings.setJavaScriptEnabled(true);
    //  页面加载好以后,再放开图片
    settings.setBlockNetworkImage(false);
    // 使用localStorage则必须打开
    settings.setDomStorageEnabled(true);
    // 排版适应屏幕
    settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
    // WebView是否支持多个窗口。
    settings.setSupportMultipleWindows(true);

    // webview从5.0开始默认不允许混合模式,https中不能加载http资源,需要设置开启。
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
    }

    /** 设置字体默认缩放大小(改变网页字体大小,setTextSize  api14被弃用)*/
    settings.setTextZoom(100);

    // Enable remote debugging via chrome://inspect
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        WebView.setWebContentsDebuggingEnabled(true);
    }

    // AppRTC requires third party cookies to work
    CookieManager cookieManager = CookieManager.getInstance();
    cookieManager.setAcceptThirdPartyCookies(webView, true);


}
 
Example 17
Source File: BaseWebSetting.java    From PocketEOS-Android with GNU Lesser General Public License v3.0 4 votes vote down vote up
private void initWebSettings() {
    WebSettings webSettings = mBaseWebView.getSettings();
    if (webSettings == null) return;
    //设置字体缩放倍数,默认100
    webSettings.setTextZoom(100);
    // 支持 Js 使用
    webSettings.setJavaScriptEnabled(true);
    // 开启DOM缓存
    webSettings.setDomStorageEnabled(true);
    // 开启数据库缓存
    webSettings.setDatabaseEnabled(true);
    // 支持自动加载图片
    webSettings.setLoadsImagesAutomatically(hasKitkat());
    if (isCache) {
        // 设置 WebView 的缓存模式
        webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
        // 支持启用缓存模式
        webSettings.setAppCacheEnabled(true);
        // 设置 AppCache 最大缓存值(现在官方已经不提倡使用,已废弃)
        webSettings.setAppCacheMaxSize(8 * 1024 * 1024);
        // Android 私有缓存存储,如果你不调用setAppCachePath方法,WebView将不会产生这个目录
        webSettings.setAppCachePath(mContext.getCacheDir().getAbsolutePath());
    }
    // 数据库路径
    if (!hasKitkat()) {
        webSettings.setDatabasePath(mContext.getDatabasePath("html").getPath());
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
    }
    // 关闭密码保存提醒功能
    webSettings.setSavePassword(false);
    // 支持缩放
    webSettings.setSupportZoom(true);
    // 设置 UserAgent 属性
    webSettings.setUserAgentString("");
    // 允许加载本地 html 文件/false
    webSettings.setAllowFileAccess(true);
    // 允许通过 file url 加载的 Javascript 读取其他的本地文件,Android 4.1 之前默认是true,在 Android 4.1 及以后默认是false,也就是禁止
    webSettings.setAllowFileAccessFromFileURLs(false);
    // 允许通过 file url 加载的 Javascript 可以访问其他的源,包括其他的文件和 http,https 等其他的源,
    // Android 4.1 之前默认是true,在 Android 4.1 及以后默认是false,也就是禁止
    // 如果此设置是允许,则 setAllowFileAccessFromFileURLs 不起做用
    webSettings.setAllowUniversalAccessFromFileURLs(false);

}
 
Example 18
Source File: PageDetailActivity.java    From MaoWanAndoidClient with Apache License 2.0 4 votes vote down vote up
private void initWebView() {
        mProgressBar.setVisibility(View.VISIBLE);
        WebSettings ws = webView.getSettings();
        // 网页内容的宽度是否可大于WebView控件的宽度
        ws.setLoadWithOverviewMode(false);
        // 保存表单数据
        ws.setSaveFormData(true);
        // 是否应该支持使用其屏幕缩放控件和手势缩放
        ws.setSupportZoom(true);
        ws.setBuiltInZoomControls(true);
        ws.setDisplayZoomControls(false);
        // 启动应用缓存
        ws.setAppCacheEnabled(true);
        // 设置缓存模式
        ws.setCacheMode(WebSettings.LOAD_DEFAULT);
        // setDefaultZoom  api19被弃用
        // 设置此属性,可任意比例缩放。
        ws.setUseWideViewPort(true);
        // 不缩放
        webView.setInitialScale(100);
        // 告诉WebView启用JavaScript执行。默认的是false。
        ws.setJavaScriptEnabled(true);
        //  页面加载好以后,再放开图片
        ws.setBlockNetworkImage(false);
        // 使用localStorage则必须打开
        ws.setDomStorageEnabled(true);
        // 排版适应屏幕
        ws.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        // WebView是否新窗口打开(加了后可能打不开网页)
//        ws.setSupportMultipleWindows(true);

        // webview从5.0开始默认不允许混合模式,https中不能加载http资源,需要设置开启。
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            ws.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        /** 设置字体默认缩放大小(改变网页字体大小,setTextSize  api14被弃用)*/
        ws.setTextZoom(100);

        mWebChromeClient = new MyWebChromeClient(this);
        webView.setWebChromeClient(mWebChromeClient);
        // 与js交互
        webView.addJavascriptInterface(new MyJavascriptInterface(this), "injectedObject");
        webView.setWebViewClient(new MyWebViewClient(this));
        webView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                return handleLongImage();
            }
        });
    }
 
Example 19
Source File: WebViewActivity.java    From YCAudioPlayer with Apache License 2.0 4 votes vote down vote up
@SuppressLint("JavascriptInterface")
private void initWebView() {
    WebSettings ws = mWebView.getSettings();
    // 网页内容的宽度是否可大于WebView控件的宽度
    ws.setLoadWithOverviewMode(false);
    // 保存表单数据
    ws.setSaveFormData(true);
    // 是否应该支持使用其屏幕缩放控件和手势缩放
    ws.setSupportZoom(true);
    ws.setBuiltInZoomControls(true);
    ws.setDisplayZoomControls(false);
    // 启动应用缓存
    ws.setAppCacheEnabled(true);
    // 设置缓存模式
    ws.setCacheMode(WebSettings.LOAD_DEFAULT);
    // setDefaultZoom  api19被弃用
    // 设置此属性,可任意比例缩放。
    ws.setUseWideViewPort(true);
    // 缩放比例 1
    mWebView.setInitialScale(1);
    // 告诉WebView启用JavaScript执行。默认的是false。
    ws.setJavaScriptEnabled(true);
    //如果启用了JavaScript,要做好安全措施,防止远程执行漏洞
    removeJavascriptInterfaces(mWebView);
    //  页面加载好以后,再放开图片
    ws.setBlockNetworkImage(false);
    // 使用localStorage则必须打开
    ws.setDomStorageEnabled(true);
    //自动加载图片
    ws.setLoadsImagesAutomatically(true);
    // 排版适应屏幕
    ws.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
    // WebView是否支持多个窗口。
    ws.setSupportMultipleWindows(true);
    // webView从5.0开始默认不允许混合模式,https中不能加载http资源,需要设置开启。
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        ws.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
    }
    /*设置字体默认缩放大小(改变网页字体大小,setTextSize  api14被弃用)*/
    ws.setTextZoom(100);
    //在js中调用本地java方法
    jsAppInterface = new JsAppInterface(this, mWebView);
    jsAppInterface.register();
    mWebView.addJavascriptInterface(jsAppInterface, "WebViewJsMethodName");
    mWebView.addJavascriptInterface(new JavascriptInterface(this), "injectedObject");
    mWebView.setWebViewClient(new MyWebViewClient());
    mWebView.setWebChromeClient(webChromeClient = new MyWebChromeClient());
    mWebView.setScrollWebListener(new ScrollWebView.OnScrollWebListener() {
        @Override
        public void onScroll(int dx, int dy) {
            //WebView的总高度
            float webViewContentHeight = mWebView.getContentHeight() * mWebView.getScale();
            //WebView的现高度
            float webViewCurrentHeight = (mWebView.getHeight() + mWebView.getScrollY());
            LogUtils.e("webViewContentHeight=" + webViewContentHeight);
            LogUtils.e("webViewCurrentHeight=" + webViewCurrentHeight);
            if ((webViewContentHeight - webViewCurrentHeight) == 0) {
                LogUtils.e("WebView滑动到了底端");
            }
        }
    });
}
 
Example 20
Source File: BookPageFragment.java    From IslamicLibraryAndroid with GNU General Public License v3.0 3 votes vote down vote up
private void initializeWebView(@NonNull WebView webView, @NonNull WebSettings webSettings) {
    tashkeelOn = pageFragmentListener.getTashkeelState();
    if (!tashkeelOn) page_content = ArabicUtilities.cleanTashkeel(page_content);

    boolean isNightMode = pageFragmentListener.isNightMode();

    int intialZoom = pageFragmentListener.getDisplayZoom();
    webSettings.setTextZoom(intialZoom);


    String data = prepareHtml(isNightMode);
    loadWebView(data, webView);
    if (isNightMode) webView.setBackgroundColor(Color.TRANSPARENT);

}