使用androidx后,在布局中引用webview,項(xiàng)目異常崩潰。?
一俩块、自定義webVIew
public class LollipopFixedWebView extends WebView {
? ? public LollipopFixedWebView(Context context) {
? ? ? ? super(getFixedContext(context));
? ? }
? ? public LollipopFixedWebView(Context context, AttributeSet attrs) {
? ? ? ? super(getFixedContext(context), attrs);
? ? }
? ? public LollipopFixedWebView(Context context, AttributeSet attrs, int defStyleAttr) {
? ? ? ? super(getFixedContext(context), attrs, defStyleAttr);
? ? }
? ? @TargetApi(Build.VERSION_CODES.LOLLIPOP)
? ? public LollipopFixedWebView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
? ? ? ? super(getFixedContext(context), attrs, defStyleAttr, defStyleRes);
? ? }
? ? private static Context getFixedContext(Context context) {
? ? ? ? if (Build.VERSION.SDK_INT >= 21 && Build.VERSION.SDK_INT < 23) // Android Lollipop 5.0 & 5.1
? ? ? ? ? ? return context.createConfigurationContext(new Configuration());
? ? ? ? return context;
? ? }
}
二、回退版本
dependencies {
? ? def appcompat_version = "1.1.0"
? ? implementation "androidx.appcompat:appcompat:$appcompat_version"
? ? // For loading and tinting drawables on older versions of the platform
? ? implementation "androidx.appcompat:appcompat-resources:$appcompat_version"
}
三浓领、配置AndroidManifest文件?
在當(dāng)前頁面的AndroidManifest文件中玉凯,找到使用webview的頁面 加入android:configChanges="uiMode"
四、
new WebView(getActivity().createConfigurationContext(new Configuration()));