setLayerType替代Android 2.3.3?

Jus*_*sLi 1 android android-layout android-view

如果我使用不属于Android 2.3.3 API的视图类(如setLayerType)的API,如何让我的Android应用程序与以前的Android版本兼容?我可以用这种方法代替什么?

Bla*_*elt 5

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
     // only for gingerbread and newer versions
//        setLayerType
}
Run Code Online (Sandbox Code Playgroud)

或通过反思

try {
    Method setLayerTypeMethod = mWebView.getClass().getMethod("setLayerType", new Class[] {int.class, Paint.class});
    if (setLayerTypeMethod != null)
         setLayerTypeMethod.invoke(yourView, new Object[] {LAYER_TYPE_SOFTWARE, null});
} catch (NoSuchMethodException e) {
   e.printStackTrace();   
} catch (IllegalArgumentException e) {
    e.printStackTrace();
} catch (IllegalAccessException e) {
    e.printStackTrace();
} catch (InvocationTargetException e) {
    e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)

  • 你不能在ginberbread上有这个功能.它是自HoneyComb以来推出的 (2认同)
  • 正如黑带指出的那样,将GINGERBREAD更改为HONEYCOMB并将>更改为> = (2认同)