Android的setLayerType web视图视图、Android、setLayerType、web

2023-09-08 09:43:07 作者:大叔、

我想使用动态以下code创建一个web视图:

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

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

mWebView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

mWebView.setHorizontalScrollBarEnabled(false);
mWebView.setWebViewClient(mWebViewClient);
mWebView.setWebChromeClient(mWebChromeClient);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(false);

但是,当我运行程序时,我的应用程序的力量退出,显示一个错误,有没有这样的方法,setLayerType。然而,当我通过XML创建web视图,似乎是没有问题的:

But, when I run the program, my app force quits stating an error that there is no such method as 'setLayerType'. However, when I create the Webview via the xml, there seems to be no problem:

<WebView android:id="@+id/webview"
    android:scrollbars="none"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layerType="software" />

我用的是'layertype属性在这里和应用程序运行正常。有人可以请解释这种差异?有没有办法来动态设置的WebView的图层类型?

I use the 'layertype' attribute here and the app runs fine. Can somebody please explain the discrepancy? Is there no way to set the layer type of a WebView dynamically?

推荐答案

注:这个问题:你建什么API级别?从这个问题非常不同的什么是你的目标的最低API级别?

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?".

由于您所描述的行为,它表明你正在与API级别> = 11和设备为API级别&LT上的测试; 11

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.

由于.setLayerType只能从API级别11日起,建设有API级别> = 11将建设好的,但如果你不使用兼容的技巧,如反射或: Compatibility.getCompatibility()setWebSettingsCache(webSettings)。 ......那么当你测试的设备是空气污染指数&LT上; 11,你会得到一个崩溃,因为不支持的方法。在另一方面,如果你测试API级别的设备上> = 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.