前言
Android换肤插件开发时,需要依赖LayoutInflater.Factory类,在Android P和 之前版本都测试ok,但放在Android Q(10)上测试就失效了。
本文记录一下失效的原因和解决的方法。
正文
在Android Q之前版本,通过如下代码可以设置的
try {
LayoutInflater layoutInflater = LayoutInflater.from(context);
Field mFactorySet = LayoutInflater.class.getDeclaredField("mFactorySet");
mFactorySet.setAccessible(true);
mFactorySet.set(layoutInflater, false);
LayoutInflaterCompat.setFactory2(layoutInflater, SkinInflaterFactory.getInstance());
} catch (Exception e) {
e.printStackTrace();
}
但是,Android Q版本或者之后的版本,使用上面代码就提示错误:
java.lang.NoSuchFieldException: No field mFactorySet in class Landroid/view/LayoutInflater; (declaration of 'android.view.LayoutInflater' appears in /system/framework/framework.jar!classes3.dex)
原因是LayoutInflater中的mFactorySet变量做了版本限制,如下,也就是最大支持Android P,之后的版本就无法使用了。
/**
* If any developer has desire to change this value, they should instead use
* {@link #cloneInContext(Context)} and set the new factory in thew newly-created
* LayoutInflater.
*/
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
private boolean mFactorySet;
网上其他大佬也遇到过,根据大佬说的方向,我这边也整理了一下,测试ok。
参考文章
联系我们

微信号:rssme_com
历史上的今天
- 《Android修改默认音量》
- 《Android 换肤setFactory2时出现No field mFactorySet in class问题解决》
- 《王小波:卡尔维诺与未来的一千年》
- 《Android动画介绍和属性介绍》
- 《java.io.FileNotFoundException: /storage/** (No such file or directory)》
- 《Android.mk 用法解析整理》
- 《The key must be an application-specific resource id》
- 《adb shell命令控制WIFI连接开启/关闭状态》
- 《[摘]adb命令行查询content-provider》