动态换肤

2020-08-20  本文已影响0人  Android刘东

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;
    }

上一篇 下一篇

猜你喜欢

热点阅读