美文网首页
动态换肤

动态换肤

作者: Android刘东 | 来源:发表于2020-08-20 14:00 被阅读0次

    github换肤地址:https://github.com/ximsfei/Android-skin-support
    原理:1)加载动态Resources资源
    2)XML:通过LayoutInflater.Factory2 通过反射对View实行一个集合的存储
    3)对View进行一系列的改变(从动态Resources资源获取
    ) 等等 mResources.getIdentifier(String name, String defType, String defPackage);

    //加载动态资源
    private String skinPackageName;//skin package name
        public Resources getSkinResources(String skinPkgPath, Context mAppContext) {
            try {
             PackageManager mPm = context.getPackageManager();
                            PackageInfo mInfo = mPm.getPackageArchiveInfo(skinPkgPath, PackageManager.GET_ACTIVITIES);
                            skinPackageName = mInfo.packageName;
                AssetManager assetManager = AssetManager.class.newInstance();
                Method addAssetPath = assetManager.getClass().getMethod("addAssetPath", String.class);
                addAssetPath.invoke(assetManager, skinPkgPath);
    
                Resources superRes = mAppContext.getResources();
                return new Resources(assetManager, superRes.getDisplayMetrics(), superRes.getConfiguration());
            } catch (Exception e) {
            }
            return null;
        }
    
    

    相关文章

      网友评论

          本文标题:动态换肤

          本文链接:https://www.haomeiwen.com/subject/ewbsjktx.html