Android setLayerType Webview

前端 未结 4 1486
抹茶落季
抹茶落季 2021-01-22 14:33

I am trying to create a WebView dynamically using the following code:

mWebView = new WebView(this);
mWebView.setId(R.id.webview);
mWebView.setVerticalScrollBarEn         


        
相关标签:
4条回答
  • 2021-01-22 14:37

    NOTE: The question "what api level do you build for?" is VERY different from the question "what is the minimum api level that you target?".

    Given the behaviour you have described, it suggests you are building with api level >=11 and testing on a device that is api level <11.

    Because .setLayerType is only available from api level 11 onwards, building with api level >=11 will build fine, but if you are not using compatibility tricks such as reflection or: Compatibility.getCompatibility().setWebSettingsCache(webSettings); ...then when you test on a device that is api level <11 you will get a crash because that method is not supported. On the other hand, if you test on a device of api level >=11 you should find it works.

    0 讨论(0)
  • 2021-01-22 14:38

    Old question, but answering anyway incase someone else finds it:

    You can call setLayerType via reflection. That way the code will run independent of OS version.

    try {
        Method setLayerTypeMethod = mWebView.getClass().getMethod("setLayerType", new Class[] {int.class, Paint.class});
        setLayerTypeMethod.invoke(mWebView, new Object[] {LAYER_TYPE_SOFTWARE, null});
    } catch (NoSuchMethodException e) {
        // Older OS, no HW acceleration anyway
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    }
    
    0 讨论(0)
  • 2021-01-22 14:44

    below code works fine Android 3.0+ but when you try this code below android 3.0 then your app forcefully closed.

    webView.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null);
    

    You try below code on your less then API 11.

    webview.setBackgroundColor(Color.parseColor("#919191"));
    

    Or

    you can also try below code which works on all API fine.

    webview.setBackgroundColor(Color.parseColor("#919191"));
    if (Build.VERSION.SDK_INT >= 11) {
        webview.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null);
    }
    

    above code use full for me.

    0 讨论(0)
  • 2021-01-22 14:58

    What API lvl are you building for? looks like .setLayerType(int, Paint) was introduced at api lvl 11.

    0 讨论(0)
提交回复
热议问题