Android 5.1 5.2 webview 闪退问题

自定义webview

/**
 * 处理Android 5.0 5.1 webview 闪退
 */
class MyWebView : WebView {

    companion object{
        private fun getFixedContext(context: Context): Context {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP_MR1) {
                return context.createConfigurationContext(Configuration())
            }
            return context
        }
    }

    constructor(context: Context) : super(getFixedContext(context))

    constructor(context: Context, attrs: AttributeSet) : super(getFixedContext(context), attrs)

    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(
        getFixedContext(context),
        attrs,
        defStyleAttr
    )

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int, defStyleRes: Int) : super(
        getFixedContext(context),
        attrs,
        defStyleAttr,
        defStyleRes
    )



}

在xml中使用自己定义的webview即可

猜你喜欢

转载自www.cnblogs.com/rchao/p/11897684.html