1

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

4

2 回答 2

5
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
     // only for gingerbread and newer versions
//        setLayerType
}

或通过反射

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();
}
于 2013-06-07T18:25:07.513 回答
0

要禁用硬件加速,您可以使用 android:hardwareAccelerated="false"

更多细节在这里:http: //developer.android.com/guide/topics/graphics/hardware-accel.html#controlling

但要注意 android:hardwareAccelerated="false" 因为你会付出应用程序性能的代价

我认为最好的交易是使用 blackbelt 描述的反射

于 2013-12-07T20:53:59.613 回答