{"id":1833,"date":"2023-03-29T21:26:26","date_gmt":"2023-03-29T13:26:26","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1833"},"modified":"2023-04-22T09:18:24","modified_gmt":"2023-04-22T01:18:24","slug":"android-hook-technology-enables-one-click-skin-changing","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/29\/android-hook-technology-enables-one-click-skin-changing\/","title":{"rendered":"Android Hook\u6280\u672f\u5b9e\u73b0\u4e00\u952e\u6362\u80a4"},"content":{"rendered":"<p>\u4ea7\u54c1\u8981\u6c42app\u91cc\u9762\u7684\u56fe\u8868\u8981\u5b9e\u73b0\u767d\u5929\u9ed1\u591c\u6a21\u5f0f\u7684\u5207\u6362\uff0c\u4ee5\u6ee1\u8db3\u4e0d\u540c\u5149\u7ebf\u4e0b\u90fd\u80fd\u4fdd\u8bc1\u8db3\u591f\u7684\u56fe\u8868\u6e05\u6670\u5ea6\u3002\u600e\u4e48\u529e\uff1f\u53ef\u80fd\u89e3\u51b3\u7684\u529e\u6cd5\u5f88\u591a\uff0c\u53ef\u4ee5\u7ed9\u56fe\u8868view\u589e\u52a0\u4e00\u4e2atoggle\u65b9\u6cd5\uff0c\u53c2\u6570String\uff0cday\/night\uff0c\u7136\u540e\u5207\u6362\u4e4b\u540e<code>postInvalidate<\/code>\u5237\u65b0\u91cd\u7ed8\u3002OK\uff0c\u53ef\u884c\uff0c\u4f46\u662f\u8fd9\u79cd\u65b9\u5f0f\u5207\u6362\u767d\u5929\u9ed1\u591c\uff0c\u53ea\u662f\u5355\u4e2aView\u4e2d\u6709\u6548\uff0c\u90a3\u4e48\u5982\u679c\u54ea\u5929\u4ea7\u54c1\u53c8\u8981\u53e6\u4e00\u4e2aView\u6362\u80a4\uff0c\u96be\u9053\u6211\u8981\u4e00\u4e2a\u4e00\u4e2a\u53bb\u5199toggle\u4e48\uff1f\u672a\u514d\u592alow\u4e86\u3002<\/p>\n<h2>\u4ec0\u4e48\u662f\u4e00\u952e\u6362\u80a4<\/h2>\n<p><!-- more --><\/p>\n<p>\u6240\u8c13&quot;\u4e00\u952e&quot;\uff0c\u5c31\u662f\u901a\u8fc7&quot;\u4e00\u4e2a&quot;\u63a5\u53e3\u7684\u8c03\u7528\uff0c\u5c31\u80fd\u5b9e\u73b0\u5168app\u8303\u56f4\u5185\u7684\u6240\u6709\u8d44\u6e90\u6587\u4ef6\u7684\u66ff\u6362\u3002\u5305\u62ec\u6587\u672c\uff0c\u989c\u8272\uff0c\u56fe\u7247\u7b49\u3002<\/p>\n<p>\u4e00\u4e9b\u6362\u80a4\u5b9e\u73b0\u65b9\u5f0f\u7684\u5bf9\u6bd4<\/p>\n<ul>\n<li>\u65b9\u68481\uff1a\u81ea\u5b9a\u4e49View\u4e2d\uff0c\u8981\u6362\u80a4\uff0c\u90a3\u5982\u540c\u5f15\u8a00\u4e2d\u6240\u8ff0\uff0ctoggle\u65b9\u6cd5\uff0cinvalidate\u91cd\u7ed8\u3002<\/li>\n<\/ul>\n<p>\u5f0a\u7aef\uff1a\u6362\u80a4\u8303\u56f4\u4ec5\u9650\u4e8e\u8fd9\u4e2aView<\/p>\n<ul>\n<li>\u65b9\u68482\uff1a\u7ed9\u9759\u6001\u53d8\u91cf\u8d4b\u503c\uff0c\u7136\u540e\u91cd\u542fActivity<\/li>\n<\/ul>\n<p>\u5982\u679c\u4e00\u4e2aActivity\u5185\u7528\u9759\u6001\u53d8\u91cf\u5b9a\u4e49\u4e86\u4e24\u79cd\u8272\u7cfb\uff0c\u90a3\u4e48\u786e\u5b9e\u662f\u53ef\u4ee5\u901a\u8fc7\u5173\u95edActivity\uff0c\u518d\u542f\u52a8\u7684\u65b9\u5f0f\uff0c\u5b9e\u73b0\u8c8c\u4f3c\u6362\u80a4\u7684\u6548\u679c\uff08\u5176\u5b9e\u662f\u91cd\u65b0\u542f\u52a8\u4e86Activity\uff09<\/p>\n<p>\u5f0a\u7aef\uff1a\u592alow\uff0c\u800c\u4e14\u5f88\u6d6a\u8d39\u8d44\u6e90<\/p>\n<p>\u4e5f\u8bb8\u8fd8\u6709\u5176\u4ed6\u65b9\u6848\u5427\uff0cView\u91cd\u7ed8\uff0c\u91cd\u542fActivity\uff0c\u90fd\u80fd\u5b9e\u73b0\uff0c\u4f46\u662f\u4ecd\u7136\u4e0d\u662f\u6700\u4f18\u96c5\u7684\u65b9\u6848\uff0c\u90a3\u4e48\uff0c\u6709\u6ca1\u6709\u4e00\u79cd\u65b9\u6848\uff0c\u80fd\u591f\u5b9e\u73b0\u5168app\u5185\u7684\u6362\u80a4\u6548\u679c\uff0c\u53c8\u4e0d\u4f1a\u50cf\u91cd\u542f Activity \u8fd9\u6837\u6d6a\u8d39\u8d44\u6e90\u5462\uff1f<\/p>\n<h2>\u754c\u9762\u4e0a\u54ea\u4e9b\u5143\u7d20\u662f\u53ef\u4ee5\u6362\u80a4\u7684<\/h2>\n<p>\u7b54\u6848\u5176\u5b9e\u5c31\u4e00\u53e5\u8bdd\uff1a\u9879\u76ee\u4ee3\u7801\u91cc\u9762res\u76ee\u5f55\u4e0b\u7684\u6240\u6709\u4e1c\u897f\uff0c\u51e0\u4e4e\u90fd\u53ef\u4ee5\u88ab\u66ff\u6362<\/p>\n<p>\u5177\u4f53\u800c\u8a00\u5c31\u662f\u5982\u4e0b\u8fd9\u4e9b<\/p>\n<ul>\n<li>\u52a8\u753b<\/li>\n<li>\u80cc\u666f\u56fe\u7247<\/li>\n<li>\u5b57\u4f53<\/li>\n<li>\u5b57\u4f53\u989c\u8272<\/li>\n<li>\u5b57\u4f53\u5927\u5c0f<\/li>\n<li>\u97f3\u9891<\/li>\n<li>\u89c6\u9891<\/li>\n<\/ul>\n<h2>\u5229\u7528HOOK\u6280\u672f\u5b9e\u73b0\u4f18\u96c5\u7684&quot;\u4e00\u952e\u6362\u80a4&quot;<\/h2>\n<p>hook\uff0c\u94a9\u5b50\uff0c\u5b89\u5353\u4e2d\u7684hook\u6280\u672f\uff0c\u5176\u5b9e\u662f\u4e00\u4e2a\u62bd\u8c61\u6982\u5ff5\uff1a\u5bf9\u7cfb\u7edf\u6e90\u7801\u7684\u4ee3\u7801\u903b\u8f91\u8fdb\u884c&quot;\u52ab\u6301&quot;\uff0c\u63d2\u5165\u81ea\u5df1\u7684\u903b\u8f91\uff0c\u7136\u540e\u653e\u884c\u3002\u6ce8\u610f\uff1ahook\u53ef\u80fd\u9891\u7e41\u4f7f\u7528java\u53cd\u5c04\u673a\u5236<\/p>\n<p>\u4e00\u952e\u6362\u80a4&quot;\u4e2d\u7684hook\u601d\u8def<\/p>\n<p>\uff081\uff09&quot;\u52ab\u6301&quot;\u7cfb\u7edf\u521b\u5efaView\u7684\u8fc7\u7a0b\uff0c\u6211\u4eec\u81ea\u5df1\u6765\u521b\u5efaView<\/p>\n<p>\u7cfb\u7edf\u539f\u672c\u81ea\u5df1\u5b58\u5728\u521b\u5efaView\u7684\u903b\u8f91\uff0c\u6211\u4eec\u8981\u4e86\u89e3\u8fd9\u90e8\u5206\u4ee3\u7801\uff0c\u4ee5\u4fbf\u4e3a\u6211\u6240\u7528<\/p>\n<p>\uff082\uff09\u6536\u96c6\u6211\u4eec\u9700\u8981\u6362\u80a4\u7684View\uff08\u7528\u81ea\u5b9a\u4e49view\u5c5e\u6027\u6765\u6807\u8bb0\u4e00\u4e2aview\u662f\u5426\u652f\u6301\u4e00\u952e\u6362\u80a4\uff09\uff0c\u4fdd\u5b58\u5230\u53d8\u91cf\u4e2d<\/p>\n<p>\u52ab\u6301\u4e86\u7cfb\u7edf\u521b\u5efaview\u7684\u903b\u8f91\u4e4b\u540e\uff0c\u6211\u4eec\u8981\u628a\u652f\u6301\u6362\u80a4\u7684\u8fd9\u4e9bview\u4fdd\u5b58\u8d77\u6765<\/p>\n<p>\uff083\uff09\u52a0\u8f7d\u5916\u90e8\u8d44\u6e90\u5305\uff0c\u8c03\u7528\u63a5\u53e3\u8fdb\u884c\u6362\u80a4<\/p>\n<p>\u5916\u90e8\u8d44\u6e90\u5305\uff0c\u662f<code>.apk<\/code>\u540e\u7f00\u7684\u4e00\u4e2a\u6587\u4ef6\uff0c\u662f\u901a\u8fc7gradle\u6253\u5305\u5f62\u6210\u7684\u3002\u91cc\u9762\u5305\u542b\u9700\u8981\u6362\u80a4\u7684\u8d44\u6e90\u6587\u4ef6\uff0c\u4f46\u662f\u5fc5\u987b\u4fdd\u8bc1\uff0c\u8981\u6362\u7684\u8d44\u6e90\u6587\u4ef6\uff0c\u548c\u539f\u5de5\u7a0b\u91cc\u9762\u7684\u6587\u4ef6\u540d\u5b8c\u5168\u76f8\u540c<\/p>\n<h2>\u76f8\u5173android\u6e90\u7801\u4e00\u89c8<\/h2>\n<p>\uff081\uff09Activity \u7684 setContentView(R.layout.XXX) \u5230\u5e95\u5728\u505a\u4ec0\u4e48\uff1f<\/p>\n<p>\u56de\u987e\u6211\u4eec\u5199app\u7684\u4e60\u60ef\uff0c\u521b\u5efaActivity\uff0c\u5199xxx.xml\uff0c\u5728Activity\u91cc\u9762<code>setContentView(R.layout.xxx)<\/code> \u6211\u4eec\u5199\u7684\u662fxml\uff0c\u6700\u7ec8\u5448\u73b0\u51fa\u6765\u7684\u662f\u4e00\u4e2a\u4e00\u4e2a\u7684\u754c\u9762\u4e0a\u7684UI\u63a7\u4ef6\uff0c\u90a3\u4e48<code>setContentView<\/code>\u5230\u5e95\u505a\u4e86\u4ec0\u4e48\u4e8b\uff0c\u4f7f\u5f97XML\u91cc\u9762\u7684\u5185\u5bb9\uff0c\u53d8\u6210\u4e86UI\u63a7\u4ef6\u5462\uff1f<\/p>\n<p>\u6e90\u7801\u7d22\u5f15\uff1a<\/p>\n<pre><code class=\"language-java\">@Override\npublic void setContentView(@LayoutRes int layoutResID) {\n    getDelegate().setContentView(layoutResID);\n}<\/code><\/pre>\n<p>OK\uff0c\u8fd9\u91cc\u66b4\u9732\u51fa\u4e86\u4e24\u4e2a\u65b9\u6cd5\uff0c<code>getDelegate()<\/code>\u548c<code>setContentView()<\/code>\u3002\u5148\u770b<code>getDelegate<\/code><\/p>\n<pre><code class=\"language-java\">@NonNull\npublic AppCompatDelegate getDelegate() {\n    if (mDelegate == null) {\n        mDelegate = AppCompatDelegate.create(this, this);\n    }\n    return mDelegate;\n}<\/code><\/pre>\n<p>\u8fd4\u56de\u4e00\u4e2a<code>AppCompatDelegate<\/code>\u5bf9\u8c61\uff0c\u8ddf\u8e2a\u5230<code>AppCompatDelegate<\/code>\u5185\u90e8\uff0c\u9605\u8bfb\u6e90\u7801\uff0c\u53ef\u4ee5\u5f97\u51fa\u4e00\u4e2a\u7ed3\u8bba\uff1a<code>AppCompatDelegate<\/code>\u662f\u66ffActivity\u751f\u6210View\u5bf9\u8c61\u7684\u59d4\u6258\u7c7b\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217<code>setContentView<\/code>\u65b9\u6cd5\uff0c\u5728Activity\u4e2d\u52a0\u5165UI\u63a7\u4ef6\u3002<\/p>\n<p>\u90a3\u5b83\u7684AppCompatDelegate\u7684setContentView\u65b9\u6cd5\u53c8\u505a\u4e86\u4ec0\u4e48\uff1f\uff0c\u627e\u5230setContentView\u7684\u5177\u4f53\u8fc7\u7a0b<\/p>\n<pre><code class=\"language-java\">@Override\npublic void setContentView(int resId) {\n    ensureSubDecor();\n    ViewGroup contentParent = mSubDecor.findViewById(android.R.id.content);\n    contentParent.removeAllViews();\n    LayoutInflater.from(mContext).inflate(resId, contentParent);\n    mAppCompatWindowCallback.getWrapped().onContentChanged();\n}<\/code><\/pre>\n<p>\uff082\uff09<code>LayoutInflater<\/code>\u8fd9\u4e2a\u7c7b\u662f\u600e\u4e48\u628a<code>layout.xml<\/code>\u7684<code>&lt;TextView&gt;<\/code>\u53d8\u6210<code>TextView<\/code>\u5bf9\u8c61\u7684\uff1f<\/p>\n<p>\u6211\u4eec\u77e5\u9053\uff0c\u6211\u4eec\u4f20\u5165\u7684\u662fint\uff0c\u662fxxx.xml\u8fd9\u4e2a\u5e03\u5c40\u6587\u4ef6\uff0c\u5728R\u6587\u4ef6\u91cc\u9762\u7684\u5bf9\u5e94int\u503c\u3002LayoutInflater\u62ff\u5230\u8fd9\u4e2aint\u4e4b\u540e\uff0c\u53c8\u5e72\u4e86\u4ec0\u4e48\u4e8b\u5462\uff1f<br \/>\n\u4e00\u8def\u7d22\u5f15\u8fdb\u53bb\uff1a\u4f1a\u53d1\u73b0\u8fd9\u4e2a\u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"language-java\">public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) {\n    synchronized (mConstructorArgs) {\n        Trace.traceBegin(Trace.TRACE_TAG_VIEW, &quot;inflate&quot;);\n\n        final Context inflaterContext = mContext;\n        final AttributeSet attrs = Xml.asAttributeSet(parser);\n        Context lastContext = (Context) mConstructorArgs[0];\n        mConstructorArgs[0] = inflaterContext;\n        View result = root;\n\n        try {\n            advanceToRootNode(parser);\n            final String name = parser.getName();\n\n            if (DEBUG) {\n                System.out.println(&quot;**************************&quot;);\n                System.out.println(&quot;Creating root view: &quot;\n                        + name);\n                System.out.println(&quot;**************************&quot;);\n            }\n\n            if (TAG_MERGE.equals(name)) {\n                if (root == null || !attachToRoot) {\n                    throw new InflateException(&quot;&lt;merge \/&gt; can be used only with a valid &quot;\n                            + &quot;ViewGroup root and attachToRoot=true&quot;);\n                }\n\n                rInflate(parser, root, inflaterContext, attrs, false);\n            } else {\n                \/\/ Temp is the root view that was found in the xml\n                final View temp = createViewFromTag(root, name, inflaterContext, attrs);\n\n                ViewGroup.LayoutParams params = null;\n\n                if (root != null) {\n                    if (DEBUG) {\n                        System.out.println(&quot;Creating params from root: &quot; +\n                                root);\n                    }\n                    \/\/ Create layout params that match root, if supplied\n                    params = root.generateLayoutParams(attrs);\n                    if (!attachToRoot) {\n                        \/\/ Set the layout params for temp if we are not\n                        \/\/ attaching. (If we are, we use addView, below)\n                        temp.setLayoutParams(params);\n                    }\n                }\n\n                if (DEBUG) {\n                    System.out.println(&quot;-----&gt; start inflating children&quot;);\n                }\n\n                \/\/ Inflate all children under temp against its context.\n                rInflateChildren(parser, temp, attrs, true);\n\n                if (DEBUG) {\n                    System.out.println(&quot;-----&gt; done inflating children&quot;);\n                }\n\n                \/\/ We are supposed to attach all the views we found (int temp)\n                \/\/ to root. Do that now.\n                if (root != null &amp;&amp; attachToRoot) {\n                    root.addView(temp, params);\n                }\n\n                \/\/ Decide whether to return the root that was passed in or the\n                \/\/ top view found in xml.\n                if (root == null || !attachToRoot) {\n                    result = temp;\n                }\n            }\n\n        } catch (XmlPullParserException e) {\n            final InflateException ie = new InflateException(e.getMessage(), e);\n            ie.setStackTrace(EMPTY_STACK_TRACE);\n            throw ie;\n        } catch (Exception e) {\n            final InflateException ie = new InflateException(\n                    getParserStateDescription(inflaterContext, attrs)\n                    + &quot;: &quot; + e.getMessage(), e);\n            ie.setStackTrace(EMPTY_STACK_TRACE);\n            throw ie;\n        } finally {\n            \/\/ Don&#039;t retain static reference on context.\n            mConstructorArgs[0] = lastContext;\n            mConstructorArgs[1] = null;\n\n            Trace.traceEnd(Trace.TRACE_TAG_VIEW);\n        }\n\n        return result;\n    }\n}<\/code><\/pre>\n<p>\u53d1\u73b0\u4e00\u4e2a\u5173\u952e\u65b9\u6cd5\uff1a<code>createViewFromTag<\/code>\uff0ctag\u662f\u6307\u7684\u4ec0\u4e48\uff1f\u5176\u5b9e\u5c31\u662fxml\u91cc\u9762\u7684\u6807\u7b7e\u5934<code>&lt;TextView ....&gt;<\/code><\/p>\n<pre><code class=\"language-java\">@UnsupportedAppUsage\nView createViewFromTag(View parent, String name, Context context, AttributeSet attrs,\n        boolean ignoreThemeAttr) {\n    if (name.equals(&quot;view&quot;)) {\n        name = attrs.getAttributeValue(null, &quot;class&quot;);\n    }\n\n    \/\/ Apply a theme wrapper, if allowed and one is specified.\n    if (!ignoreThemeAttr) {\n        final TypedArray ta = context.obtainStyledAttributes(attrs, ATTRS_THEME);\n        final int themeResId = ta.getResourceId(0, 0);\n        if (themeResId != 0) {\n            context = new ContextThemeWrapper(context, themeResId);\n        }\n        ta.recycle();\n    }\n\n    try {\n        View view = tryCreateView(parent, name, context, attrs);\n\n        if (view == null) {\n            final Object lastContext = mConstructorArgs[0];\n            mConstructorArgs[0] = context;\n            try {\n                if (-1 == name.indexOf(&#039;.&#039;)) {\n                    view = onCreateView(context, parent, name, attrs);\n                } else {\n                    view = createView(context, name, null, attrs);\n                }\n            } finally {\n                mConstructorArgs[0] = lastContext;\n            }\n        }\n\n        return view;\n    } catch (InflateException e) {\n        throw e;\n\n    } catch (ClassNotFoundException e) {\n        final InflateException ie = new InflateException(\n                getParserStateDescription(context, attrs)\n                + &quot;: Error inflating class &quot; + name, e);\n        ie.setStackTrace(EMPTY_STACK_TRACE);\n        throw ie;\n\n    } catch (Exception e) {\n        final InflateException ie = new InflateException(\n                getParserStateDescription(context, attrs)\n                + &quot;: Error inflating class &quot; + name, e);\n        ie.setStackTrace(EMPTY_STACK_TRACE);\n        throw ie;\n    }\n}<\/code><\/pre>\n<p>\u8fd9\u4e2a\u65b9\u6cd5\u67094\u4e2a\u53c2\u6570\uff0c\u610f\u4e49\u5206\u522b\u662f\uff1a<\/p>\n<ul>\n<li><code>View parent<\/code> \u7236\u7ec4\u4ef6<\/li>\n<li><code>String name<\/code> xml\u6807\u7b7e\u540d<\/li>\n<li><code>Context context<\/code> \u4e0a\u4e0b\u6587<\/li>\n<li><code>AttributeSet attrs<\/code> view\u5c5e\u6027<\/li>\n<li><code>boolean ignoreThemeAttr<\/code> \u662f\u5426\u5ffd\u7565theme\u5c5e\u6027<\/li>\n<\/ul>\n<p>\u8fdb\u5165<code>tryCreateView<\/code>\uff0c\u53d1\u73b0\u4e00\u6bb5\u5173\u952e\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-java\">@UnsupportedAppUsage(trackingBug = 122360734)\n@Nullable\npublic final View tryCreateView(@Nullable View parent, @NonNull String name,\n    @NonNull Context context,\n    @NonNull AttributeSet attrs) {\n    if (name.equals(TAG_1995)) {\n        \/\/ Let&#039;s party like it&#039;s 1995!\n        return new BlinkLayout(context, attrs);\n    }\n\n    View view;\n    if (mFactory2 != null) {\n        view = mFactory2.onCreateView(parent, name, context, attrs);\n    } else if (mFactory != null) {\n        view = mFactory.onCreateView(name, context, attrs);\n    } else {\n        view = null;\n    }\n\n    if (view == null &amp;&amp; mPrivateFactory != null) {\n        view = mPrivateFactory.onCreateView(parent, name, context, attrs);\n    }\n\n    return view;\n}<\/code><\/pre>\n<p>\u5b9e\u9645\u4e0a\uff0c\u53ef\u80fd\u6709\u4eba\u8981\u95ee\u4e86\uff0c\u4f60\u600e\u4e48\u77e5\u9053\u8fd9\u8fb9\u662f\u8d70\u7684\u54ea\u4e00\u4e2aif\u5206\u652f\u5462\uff1f<br \/>\n\u65b9\u6cd5\uff1a\u65b0\u521b\u5efa\u4e00\u4e2aProject\uff0c\u8ddf\u8e2aMainActivity onCreate\u91cc\u9762<code>setContentView()<\/code>\u4e00\u8def\u627e\u5230\u8fd9\u6bb5\u4ee3\u7801debug\uff0c\u5c31\u4f1a\u53d1\u73b0\u7cfb\u7edf\u5728\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u5c31\u4f1a\u8d70<code>Factory2<\/code>\u7684<code>onCreateView()<\/code>\uff0c\u90a3\u4e48\u8fd9\u4e2amFactory2\u5bf9\u8c61\u662f\u54ea\u6765\u7684\uff1f\u4ec0\u4e48\u65f6\u5019\u8bbe\u7f6e\u8fdb\u53bb\u7684<\/p>\n<pre><code class=\"language-java\">public abstract class LayoutInflater {\n    public void setFactory2(Factory2 factory) {\n        if (mFactorySet) {\n            throw new IllegalStateException(&quot;A factory has already been set on this LayoutInflater&quot;);\n        }\n        if (factory == null) {\n            throw new NullPointerException(&quot;Given factory can not be null&quot;);\n        }\n        mFactorySet = true;\n        if (mFactory == null) {\n            mFactory = mFactory2 = factory;\n        } else {\n            mFactory = mFactory2 = new FactoryMerger(factory, factory, mFactory, mFactory2);\n        }\n    }<\/code><\/pre>\n<pre><code class=\"language-java\">class AppCompatDelegateImpl extends AppCompatDelegate\n        implements MenuBuilder.Callback, LayoutInflater.Factory2 {\n\n    @Override\n    public void installViewFactory() {\n        LayoutInflater layoutInflater = LayoutInflater.from(mContext);\n        if (layoutInflater.getFactory() == null) {\n            LayoutInflaterCompat.setFactory2(layoutInflater, this);\n        } else {\n            if (!(layoutInflater.getFactory2() instanceof AppCompatDelegateImpl)) {\n                Log.i(TAG, &quot;The Activity&#039;s LayoutInflater already has a Factory installed&quot;\n                        + &quot; so we can not install AppCompat&#039;s&quot;);\n            }\n        }\n    }<\/code><\/pre>\n<p><code>getDelegate()<\/code>\u5f97\u5230\u7684\u5bf9\u8c61\uff0c\u548c<code>LayoutInflater<\/code>\u91cc\u9762<code>mFactory2<\/code>\u5176\u5b9e\u662f\u540c\u4e00\u4e2a\u5bf9\u8c61<\/p>\n<p>\u90a3\u4e48\u7ee7\u7eed\u8ddf\u8e2a\uff0c\u4e00\u76f4\u5230\uff1a<code>AppCompatViewInflater<\/code>\u7c7b<\/p>\n<pre><code class=\"language-java\">public abstract class LayoutInflater {\n\n    public interface Factory2 extends Factory {\n        \/**\n         * Version of {@link #onCreateView(String, Context, AttributeSet)}\n         * that also supplies the parent that the view created view will be\n         * placed in.\n         *\n         * @param parent The parent that the created view will be placed\n         * in; &lt;em&gt;note that this may be null&lt;\/em&gt;.\n         * @param name Tag name to be inflated.\n         * @param context The context the view is being created in.\n         * @param attrs Inflation attributes as specified in XML file.\n         *\n         * @return View Newly created view. Return null for the default\n         *         behavior.\n         *\/\n        @Nullable\n        View onCreateView(@Nullable View parent, @NonNull String name,\n                @NonNull Context context, @NonNull AttributeSet attrs);\n    }<\/code><\/pre>\n<pre><code class=\"language-java\">class AppCompatDelegateImpl extends AppCompatDelegate\n        implements MenuBuilder.Callback, LayoutInflater.Factory2 {\n\n    \/**\n     * From {@link LayoutInflater.Factory2}.\n     *\/\n    @Override\n    public View onCreateView(String name, Context context, AttributeSet attrs) {\n        return onCreateView(null, name, context, attrs);\n    }\n\n    @Override\n    public View createView(View parent, final String name, @NonNull Context context,\n            @NonNull AttributeSet attrs) {\n        if (mAppCompatViewInflater == null) {\n            TypedArray a = mContext.obtainStyledAttributes(R.styleable.AppCompatTheme);\n            String viewInflaterClassName =\n                    a.getString(R.styleable.AppCompatTheme_viewInflaterClass);\n            if ((viewInflaterClassName == null)\n                    || AppCompatViewInflater.class.getName().equals(viewInflaterClassName)) {\n                \/\/ Either default class name or set explicitly to null. In both cases\n                \/\/ create the base inflater (no reflection)\n                mAppCompatViewInflater = new AppCompatViewInflater();\n            } else {\n                try {\n                    Class&lt;?&gt; viewInflaterClass = Class.forName(viewInflaterClassName);\n                    mAppCompatViewInflater =\n                            (AppCompatViewInflater) viewInflaterClass.getDeclaredConstructor()\n                                    .newInstance();\n                } catch (Throwable t) {\n                    Log.i(TAG, &quot;Failed to instantiate custom view inflater &quot;\n                            + viewInflaterClassName + &quot;. Falling back to default.&quot;, t);\n                    mAppCompatViewInflater = new AppCompatViewInflater();\n                }\n            }\n        }\n\n        boolean inheritContext = false;\n        if (IS_PRE_LOLLIPOP) {\n            inheritContext = (attrs instanceof XmlPullParser)\n                    \/\/ If we have a XmlPullParser, we can detect where we are in the layout\n                    ? ((XmlPullParser) attrs).getDepth() &gt; 1\n                    \/\/ Otherwise we have to use the old heuristic\n                    : shouldInheritContext((ViewParent) parent);\n        }\n\n        return mAppCompatViewInflater.createView(parent, name, context, attrs, inheritContext,\n                IS_PRE_LOLLIPOP, \/* Only read android:theme pre-L (L+ handles this anyway) *\/\n                true, \/* Read read app:theme as a fallback at all times for legacy reasons *\/\n                VectorEnabledTintResources.shouldBeUsed() \/* Only tint wrap the context if enabled *\/\n        );\n    }<\/code><\/pre>\n<pre><code class=\"language-java\">public class AppCompatViewInflater {\n\n    final View createView(View parent, final String name, @NonNull Context context,\n            @NonNull AttributeSet attrs, boolean inheritContext,\n            boolean readAndroidTheme, boolean readAppTheme, boolean wrapContext) {\n        final Context originalContext = context;\n\n        \/\/ We can emulate Lollipop&#039;s android:theme attribute propagating down the view hierarchy\n        \/\/ by using the parent&#039;s context\n        if (inheritContext &amp;&amp; parent != null) {\n            context = parent.getContext();\n        }\n        if (readAndroidTheme || readAppTheme) {\n            \/\/ We then apply the theme on the context, if specified\n            context = themifyContext(context, attrs, readAndroidTheme, readAppTheme);\n        }\n        if (wrapContext) {\n            context = TintContextWrapper.wrap(context);\n        }\n\n        View view = null;\n\n        \/\/ We need to &#039;inject&#039; our tint aware Views in place of the standard framework versions\n        switch (name) {\n            case &quot;TextView&quot;:\n                view = createTextView(context, attrs);\n                verifyNotNull(view, name);\n                break;\n            case &quot;ImageView&quot;:\n                view = createImageView(context, attrs);\n                verifyNotNull(view, name);\n                break;\n            case &quot;Button&quot;:\n                view = createButton(context, attrs);\n                verifyNotNull(view, name);\n                break;\n            case &quot;EditText&quot;:\n                view = createEditText(context, attrs);\n                verifyNotNull(view, name);\n                break;\n            case &quot;Spinner&quot;:\n                view = createSpinner(context, attrs);\n                verifyNotNull(view, name);\n                break;\n            case &quot;ImageButton&quot;:\n                view = createImageButton(context, attrs);\n                verifyNotNull(view, name);\n                break;\n            case &quot;CheckBox&quot;:\n                view = createCheckBox(context, attrs);\n                verifyNotNull(view, name);\n                break;\n            case &quot;RadioButton&quot;:\n                view = createRadioButton(context, attrs);\n                verifyNotNull(view, name);\n                break;\n            case &quot;CheckedTextView&quot;:\n                view = createCheckedTextView(context, attrs);\n                verifyNotNull(view, name);\n                break;\n            case &quot;AutoCompleteTextView&quot;:\n                view = createAutoCompleteTextView(context, attrs);\n                verifyNotNull(view, name);\n                break;\n            case &quot;MultiAutoCompleteTextView&quot;:\n                view = createMultiAutoCompleteTextView(context, attrs);\n                verifyNotNull(view, name);\n                break;\n            case &quot;RatingBar&quot;:\n                view = createRatingBar(context, attrs);\n                verifyNotNull(view, name);\n                break;\n            case &quot;SeekBar&quot;:\n                view = createSeekBar(context, attrs);\n                verifyNotNull(view, name);\n                break;\n            case &quot;ToggleButton&quot;:\n                view = createToggleButton(context, attrs);\n                verifyNotNull(view, name);\n                break;\n            default:\n                \/\/ The fallback that allows extending class to take over view inflation\n                \/\/ for other tags. Note that we don&#039;t check that the result is not-null.\n                \/\/ That allows the custom inflater path to fall back on the default one\n                \/\/ later in this method.\n                view = createView(context, name, attrs);\n        }\n\n        if (view == null &amp;&amp; originalContext != context) {\n            \/\/ If the original context does not equal our themed context, then we need to manually\n            \/\/ inflate it using the name so that android:theme takes effect.\n            view = createViewFromTag(context, name, attrs);\n        }\n\n        if (view != null) {\n            \/\/ If we have created a view, check its android:onClick\n            checkOnClickListener(view, attrs);\n        }\n\n        return view;\n    }<\/code><\/pre>\n<p>\u8fd9\u8fb9\u5229\u7528\u4e86\u5927\u91cf\u7684<code>switch case<\/code>\u6765\u8fdb\u884c\u7cfb\u7edf\u63a7\u4ef6\u7684\u521b\u5efa\uff0c\u4f8b\u5982\uff1aTextView<\/p>\n<pre><code class=\"language-java\">@NonNull\nprotected AppCompatTextView createTextView(Context context, AttributeSet attrs) {\n    return new AppCompatTextView(context, attrs);\n}<\/code><\/pre>\n<p>\u90fd\u662fnew \u51fa\u6765\u4e00\u4e2a\u5177\u6709\u517c\u5bb9\u7279\u6027\u7684TextView\uff0c\u8fd4\u56de\u51fa\u53bb<br \/>\n\u4f46\u662f\uff0c\u4f7f\u7528\u8fc7switch \u7684\u4eba\u90fd\u77e5\u9053\uff0c\u8fd9\u79cdcase\u5f62\u5f0f\u7684\u5206\u652f\uff0c\u65e0\u6cd5\u6db5\u76d6\u6240\u6709\u7684\u7c7b\u578b\u600e\u4e48\u529e\u5462\uff1f\u8fd9\u91ccswitch\u4e4b\u540e\uff0cview\u4ecd\u7136\u53ef\u80fd\u662fnull<br \/>\n\u6240\u4ee5\uff0cswitch\u4e4b\u540e\uff0c\u8c37\u6b4c\u5927\u4f6c\u52a0\u4e86\u4e00\u4e2aif\uff0c\u4f46\u662f\u5f88\u8be1\u5f02\uff0c\u8fd9\u6bb5\u4ee3\u7801\u5e76\u672a\u8fdb\u5165if\uff0c\u56e0\u4e3a<code>originalContext != context<\/code>\u5e76\u4e0d\u6ee1\u8db3&#8230;.\u5177\u4f53\u539f\u56e0\u6682\u65f6\u4e5f\u6ca1\u67e5\u51fa\u6765<\/p>\n<pre><code class=\"language-java\">if (view == null &amp;&amp; originalContext != context) {\n    \/\/ If the original context does not equal our themed context, then we need to manually\n    \/\/ inflate it using the name so that android:theme takes effect.\n    view = createViewFromTag(context, name, attrs);\n}<\/code><\/pre>\n<p>\u7136\u800c\uff0c\u8fd9\u91cc\u7684\u8865\u6551\u63aa\u65bd\u6ca1\u6709\u6267\u884c\uff0c\u90a3\u81ea\u7136\u6709\u5730\u65b9\u6709\u53e6\u5916\u7684\u8865\u6551\u63aa\u65bd\u3002\u56de\u5230\u4e4b\u524d\u7684<code>LayoutInflater<\/code>\u7684\u4e0b\u9762\u8fd9\u6bb5\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-java\">View view;\nif (mFactory2 != null) {\n    view = mFactory2.onCreateView(parent, name, context, attrs);\n} else if (mFactory != null) {\n    view = mFactory.onCreateView(name, context, attrs);\n} else {\n    view = null;\n}<\/code><\/pre>\n<pre><code class=\"language-java\">View view = tryCreateView(parent, name, context, attrs);\n\nif (view == null) {\n    final Object lastContext = mConstructorArgs[0];\n    mConstructorArgs[0] = context;\n    try {\n        if (-1 == name.indexOf(&#039;.&#039;)) {\n            view = onCreateView(context, parent, name, attrs);\n        } else {\n            view = createView(context, name, null, attrs);\n        }\n    } finally {\n        mConstructorArgs[0] = lastContext;\n    }\n}\n\nreturn view;<\/code><\/pre>\n<p>\u8fd9\u91cc\u7684\u4e24\u4e2a\u65b9\u6cd5<code>onCreateView(parent, name, attrs)<\/code>\u548c<code>createView(name, null, attrs)<\/code>\u90fd\u6700\u7ec8\u7d22\u5f15\u5230\uff1a<\/p>\n<pre><code class=\"language-java\">public final View createView(@NonNull Context viewContext, @NonNull String name,\n        @Nullable String prefix, @Nullable AttributeSet attrs)\n        throws ClassNotFoundException, InflateException {\n    Objects.requireNonNull(viewContext);\n    Objects.requireNonNull(name);\n    Constructor&lt;? extends View&gt; constructor = sConstructorMap.get(name);\n    if (constructor != null &amp;&amp; !verifyClassLoader(constructor)) {\n        constructor = null;\n        sConstructorMap.remove(name);\n    }\n    Class&lt;? extends View&gt; clazz = null;\n\n    try {\n        Trace.traceBegin(Trace.TRACE_TAG_VIEW, name);\n\n        if (constructor == null) {\n            \/\/ Class not found in the cache, see if it&#039;s real, and try to add it\n            clazz = Class.forName(prefix != null ? (prefix + name) : name, false,\n                    mContext.getClassLoader()).asSubclass(View.class);\n\n            if (mFilter != null &amp;&amp; clazz != null) {\n                boolean allowed = mFilter.onLoadClass(clazz);\n                if (!allowed) {\n                    failNotAllowed(name, prefix, viewContext, attrs);\n                }\n            }\n            constructor = clazz.getConstructor(mConstructorSignature);\n            constructor.setAccessible(true);\n            sConstructorMap.put(name, constructor);\n        } else {\n            \/\/ If we have a filter, apply it to cached constructor\n            if (mFilter != null) {\n                \/\/ Have we seen this name before?\n                Boolean allowedState = mFilterMap.get(name);\n                if (allowedState == null) {\n                    \/\/ New class -- remember whether it is allowed\n                    clazz = Class.forName(prefix != null ? (prefix + name) : name, false,\n                            mContext.getClassLoader()).asSubclass(View.class);\n\n                    boolean allowed = clazz != null &amp;&amp; mFilter.onLoadClass(clazz);\n                    mFilterMap.put(name, allowed);\n                    if (!allowed) {\n                        failNotAllowed(name, prefix, viewContext, attrs);\n                    }\n                } else if (allowedState.equals(Boolean.FALSE)) {\n                    failNotAllowed(name, prefix, viewContext, attrs);\n                }\n            }\n        }\n\n        Object lastContext = mConstructorArgs[0];\n        mConstructorArgs[0] = viewContext;\n        Object[] args = mConstructorArgs;\n        args[1] = attrs;\n\n        try {\n            final View view = constructor.newInstance(args);\n            if (view instanceof ViewStub) {\n                \/\/ Use the same context when inflating ViewStub later.\n                final ViewStub viewStub = (ViewStub) view;\n                viewStub.setLayoutInflater(cloneInContext((Context) args[0]));\n            }\n            return view;\n        } finally {\n            mConstructorArgs[0] = lastContext;\n        }\n    } catch (NoSuchMethodException e) {\n        final InflateException ie = new InflateException(\n                getParserStateDescription(viewContext, attrs)\n                + &quot;: Error inflating class &quot; + (prefix != null ? (prefix + name) : name), e);\n        ie.setStackTrace(EMPTY_STACK_TRACE);\n        throw ie;\n\n    } catch (ClassCastException e) {\n        \/\/ If loaded class is not a View subclass\n        final InflateException ie = new InflateException(\n                getParserStateDescription(viewContext, attrs)\n                + &quot;: Class is not a View &quot; + (prefix != null ? (prefix + name) : name), e);\n        ie.setStackTrace(EMPTY_STACK_TRACE);\n        throw ie;\n    } catch (ClassNotFoundException e) {\n        \/\/ If loadClass fails, we should propagate the exception.\n        throw e;\n    } catch (Exception e) {\n        final InflateException ie = new InflateException(\n                getParserStateDescription(viewContext, attrs) + &quot;: Error inflating class &quot;\n                        + (clazz == null ? &quot;&lt;unknown&gt;&quot; : clazz.getName()), e);\n        ie.setStackTrace(EMPTY_STACK_TRACE);\n        throw ie;\n    } finally {\n        Trace.traceEnd(Trace.TRACE_TAG_VIEW);\n    }\n}<\/code><\/pre>\n<p>\u8fd9\u4e48\u4e00\u5927\u6bb5\u597d\u50cf\u6709\u70b9\u8ba9\u4eba\u5bb3\u6015\u3002\u5176\u5b9e\u771f\u6b63\u9700\u8981\u5173\u6ce8\u7684\uff0c\u5c31\u662f\u53cd\u5c04\u7684\u4ee3\u7801\uff0c\u6700\u540e\u7684<code>newInstance()<\/code><\/p>\n<p>\uff083\uff09APP\u4e2d\u8d44\u6e90\u6587\u4ef6\u5927\u7ba1\u5bb6<code>Resources\/AssetManager<\/code>\u662f\u600e\u4e48\u5de5\u4f5c\u7684<\/p>\n<p>\u4ece\u6211\u4eec\u7684\u7ec8\u6781\u76ee\u7684\u51fa\u53d1\uff1a\u6211\u4eec\u8981\u505a\u7684\u662f\u201c\u6362\u80a4\u201d\uff0c\u5982\u679c\u6211\u4eec\u62ff\u5230\u4e86\u8981\u6362\u80a4\u7684View\uff0c\u53ef\u4ee5\u5bf9\u4ed6\u4eec\u8fdb\u884csetXXX\u5c5e\u6027\u6765\u6539\u53d8UI\uff0c\u90a3\u4e48\u5c5e\u6027\u503c\u4ece\u54ea\u91cc\u6765?<br \/>\n\u754c\u9762\u5143\u7d20\u4e30\u5bcc\u591a\u5f69\uff0c\u4f46\u662f\u8fd9\u4e9bView\uff0c\u90fd\u662f\u7528\u8d44\u6e90\u6587\u4ef6\u6765\u8fdb\u884c&quot;\u88c5\u626e&quot;\u51fa\u6765\u7684\uff0c\u8d44\u6e90\u6587\u4ef6\u5927\u81f4\u53ef\u4ee5\u5206\u4e3a\uff1a\u56fe\u7247\u3001\u6587\u5b57\u3001\u989c\u8272\u3001\u58f0\u97f3\u89c6\u9891\u3001\u5b57\u4f53\u7b49\u3002\u5982\u679c\u6211\u4eec\u63a7\u5236\u4e86\u8d44\u6e90\u6587\u4ef6\uff0c\u90a3\u4e48\u662f\u4e0d\u662f\u6709\u80fd\u529b\u5bf9\u754c\u9762\u5143\u7d20\u8fdb\u884cset\u67d0\u67d0\u5c5e\u6027\u6765\u8fdb\u884c\u201c\u518d\u88c5\u626e\u201d\u5462\uff1f<\/p>\n<p>\u5f53\u7136\uff0c\u8fd9\u662f\u53ef\u884c\u7684\u3002\u56e0\u4e3a\uff0c\u6211\u4eec\u5e73\u65f6\u62ff\u5230\u4e00\u4e2aTextView\uff0c\u5c31\u80fd\u5bf9\u5b83\u8fdb\u884c<code>setTextColor<\/code>\uff0c\u8fd9\u79cd\u64cd\u4f5c\uff0c\u5728View\u8fd8\u5b58\u6d3b\u7684\u65f6\u5019\uff0c\u90fd\u53ef\u4ee5\u8fdb\u884c\u64cd\u4f5c\uff0c\u5e76\u4e14\u8fd9\u79cd\u64cd\u4f5c\uff0c\u5e76\u4e0d\u4f1a\u9020\u6210Activity\u7684\u91cd\u542f\u3002<\/p>\n<p>\u8fd9\u4e9b\u8d44\u6e90\u6587\u4ef6\uff0c\u6709\u4e00\u4e2a\u7edf\u4e00\u7684\u5927\u7ba1\u5bb6\u3002\u53ef\u80fd\u6709\u4eba\u8bf4\u662f<code>R.java<\/code>\u6587\u4ef6\uff0c\u5b83\u91cc\u9762\u7edf\u7b79\u4e86\u6240\u6709\u7684\u8d44\u6e90\u6587\u4ef6int\u503c\u3002\u6ca1\u9519\uff0c\u4f46\u662f\u8fd9\u4e2aR\u6587\u4ef6\u662f\u5982\u4f55\u4ea7\u751f\u4f5c\u7528\u7684\u5462\uff1f\u7b54\u6848\uff1a<code>Resources<\/code><\/p>\n<h2>\u5168APP\u4e00\u952e\u6362\u80a4\u5b9e\u73b0<\/h2>\n<h3>\u9879\u76ee\u5de5\u7a0b\u7ed3\u6784<\/h3>\n<ul>\n<li><code>app module<\/code>: <code>applicationId &quot;cn.appblog.skindemo&quot;<\/code><\/li>\n<li><code>blue_skin_plugin module<\/code>: <code>apply plugin: &#039;com.android.application&#039;<\/code>, <code>applicationId &quot;cn.appblog.skindemo.skin.blue_skin_plugin&quot;<\/code><\/li>\n<li><code>orange_skin_plugin module<\/code>: <code>apply plugin: &#039;com.android.application&#039;<\/code>, <code>applicationId &quot;cn.appblog.skindemo.skin.orange_skin_plugin&quot;<\/code><\/li>\n<\/ul>\n<blockquote>\n<p>\u76ae\u80a4\u5305<code>blue_skin_plugin module<\/code>\u548c<code>orange_skin_plugin module<\/code>\u91cc\u9762\uff0c\u53ea\u63d0\u4f9b\u9700\u8981\u6362\u80a4\u7684\u8d44\u6e90\u5373\u53ef\uff0c\u4e0d\u9700\u8981\u6362\u80a4\u7684\u8d44\u6e90\uff08\u5982layout\u3001attr\uff09\u4ee5\u53casrc\u76ee\u5f55\u4e0b\u7684Java\u6e90\u7801\u4e0d\u8981\u653e\u5728\u8fd9\u91cc\uff0c\u4ee5\u514d\u65e0\u7aef\u589e\u5927\u76ae\u80a4\u5305\u7684\u4f53\u79ef<\/p>\n<p>\u6ce8\u610f\uff1a<code>blue_skin_plugin module<\/code>\u53ca<code>orange_skin_plugin module<\/code>\u7684\u8d44\u6e90\u6587\u4ef6\uff08\u5982color\u3001drawable\u3001mipmap\u3001styles\uff09\u5e94\u4e0e<code>app module<\/code>\u4fdd\u6301\u4e00\u81f4\uff08\u6587\u4ef6\u6216\u8d44\u6e90Key\u4e00\u81f4\uff09\uff0c\u4ee5\u786e\u4fdd\u5176\u751f\u6210\u7684id\u76f8\u540c<\/p>\n<p>\u76ae\u80a4\u5305<code>blue_skin_plugin module<\/code>\u548c<code>orange_skin_plugin module<\/code>\u7684gradle sdk\u7248\u672c\u6700\u597d\u548c<code>app module<\/code>\u7684\u4fdd\u6301\u5b8c\u5168\u4e00\u81f4\uff0c\u5426\u5219\u65e0\u6cd5\u4fdd\u8bc1\u4e0d\u4f1a\u51fa\u73b0\u5947\u8469\u95ee\u9898<\/p>\n<p>\u4f7f\u7528\u76ae\u80a4\u5305<code>blue_skin_plugin module<\/code>\u548c<code>orange_skin_plugin module<\/code>\u6253\u5305\u751f\u6210\u7684apk\u6587\u4ef6\uff0c\u5e38\u89c4\u6765\u8bf4\uff0c\u662f\u901a\u8fc7\u7f51\u7edc\u4e0b\u8f7d\u5e76\u52a0\u8f7d\uff0c\u4e3a\u8c03\u8bd5\u65b9\u4fbf\u653e\u5728\u624b\u673aSD\u5361\u4e2d\uff0c\u7136\u540e\u7531<code>app module<\/code>\u5185\u7684\u4ee3\u7801\u53bb\u52a0\u8f7d<\/p>\n<\/blockquote>\n<h3>\u76ae\u80a4\u8d44\u6e90\u914d\u7f6e<\/h3>\n<p><strong>\u786e\u4fdd\u8d44\u6e90\u6587\u4ef6\uff08color\u3001drawable\u3001mipmap\u3001styles\uff09\u4e0e<code>app module<\/code>\u4fdd\u6301\u4e00\u81f4\uff08\u6587\u4ef6\u6216\u8d44\u6e90Key\u4e00\u81f4\uff09<\/strong><\/p>\n<p>\u8981\u6ce8\u610f\uff1a\u6253\u4e24\u4e2a\u76ae\u80a4\u5305\u8fd0\u884cdemo\uff0c\u6253\u5305\u4e4b\u524d\uff0c\u4e00\u5b9a\u8981\u8bb0\u5f97\u66ff\u6362drawable\u56fe\u7247\u8d44\u6e90\u4e3a\u540c\u540d\u6587\u4ef6<\/p>\n<h4>app module<\/h4>\n<p>colors.xml<\/p>\n<pre><code class=\"language-xml\">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;\n&lt;resources&gt;\n    &lt;color name=&quot;colorPrimary&quot;&gt;#008577&lt;\/color&gt;\n    &lt;color name=&quot;colorPrimaryDark&quot;&gt;#00574B&lt;\/color&gt;\n    &lt;color name=&quot;colorAccent&quot;&gt;#00574B&lt;\/color&gt;\n&lt;\/resources&gt;<\/code><\/pre>\n<p>attrs.xml<\/p>\n<pre><code class=\"language-xml\">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;\n&lt;resources&gt;&lt;!--TODO: \u5173\u952e\u6280\u672f\u70b92 \u901a\u8fc7\u81ea\u5b9a\u4e49\u5c5e\u6027\u6765\u6807\u8bc6\u54ea\u4e9bview\u652f\u6301\u6362\u80a4--&gt;\n    &lt;declare-styleable name=&quot;Skinable&quot;&gt;\n        &lt;!--TODO: isSupport=true\u6807\u8bc6\u5f53\u524d\u63a7\u4ef6\u652f\u6301\u6362\u80a4--&gt;\n        &lt;attr name=&quot;isSupport&quot; format=&quot;boolean&quot; \/&gt;\n    &lt;\/declare-styleable&gt;\n&lt;\/resources&gt;<\/code><\/pre>\n<h4>blue_skin_plugin module<\/h4>\n<pre><code class=\"language-xml\">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;\n&lt;resources&gt;\n    &lt;color name=&quot;colorPrimary&quot;&gt;#2CB6F3&lt;\/color&gt;\n    &lt;color name=&quot;colorPrimaryDark&quot;&gt;#00A0E9&lt;\/color&gt;\n    &lt;color name=&quot;colorAccent&quot;&gt;#00A0E9&lt;\/color&gt;\n&lt;\/resources&gt;<\/code><\/pre>\n<h4>orange_skin_plugin module<\/h4>\n<pre><code class=\"language-xml\">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;\n&lt;resources&gt;\n    &lt;color name=&quot;colorPrimary&quot;&gt;#EA6f5A&lt;\/color&gt;\n    &lt;color name=&quot;colorPrimaryDark&quot;&gt;#EC7259&lt;\/color&gt;\n    &lt;color name=&quot;colorAccent&quot;&gt;#EC7259&lt;\/color&gt;\n&lt;\/resources&gt;<\/code><\/pre>\n<h3>\u5173\u952e\u7c7b SkinFactory<\/h3>\n<p><code>SkinFactory<\/code>\u7c7b\uff0c\u7ee7\u627f<code>LayoutInflater.Factory2<\/code>\uff0c\u5b83\u7684\u5b9e\u4f8b\uff0c\u4f1a\u8d1f\u8d23\u521b\u5efaView\uff0c\u6536\u96c6\u652f\u6301\u6362\u80a4\u7684View<\/p>\n<pre><code class=\"language-java\">package cn.appblog.skindemo.skin;\n\nimport android.content.Context;\nimport android.content.res.TypedArray;\nimport android.text.TextUtils;\nimport android.util.AttributeSet;\nimport android.util.Log;\nimport android.view.LayoutInflater;\nimport android.view.View;\nimport android.widget.TextView;\n\nimport androidx.appcompat.app.AppCompatDelegate;\n\nimport java.lang.reflect.Constructor;\nimport java.util.ArrayList;\nimport java.util.HashMap;\nimport java.util.List;\n\nimport cn.appblog.skindemo.R;\nimport cn.appblog.skindemo.view.ZeroView;\n\npublic class SkinFactory implements LayoutInflater.Factory2 {\n\n    private AppCompatDelegate mDelegate; \/\/\u9884\u5b9a\u4e49\u4e00\u4e2a\u59d4\u6258\u7c7b\uff0c\u5b83\u8d1f\u8d23\u6309\u7167\u7cfb\u7edf\u7684\u539f\u6709\u903b\u8f91\u6765\u521b\u5efaView\n\n    private List&lt;SkinView&gt; listCacheSkinView = new ArrayList&lt;&gt;(); \/\/\u81ea\u5b9a\u4e49\u7684list\uff0c\u7f13\u5b58\u6240\u6709\u53ef\u4ee5\u6362\u80a4\u7684View\u5bf9\u8c61\n\n    \/**\n     * \u7ed9\u5916\u90e8\u63d0\u4f9b\u4e00\u4e2aset\u65b9\u6cd5\n     *\n     * @param mDelegate\n     *\/\n    public void setDelegate(AppCompatDelegate mDelegate) {\n        this.mDelegate = mDelegate;\n    }\n\n    \/**\n     * Factory2 \u662f\u7ee7\u627fFactory\u7684\uff0c\u6240\u4ee5\uff0c\u6211\u4eec\u8fd9\u6b21\u662f\u4e3b\u8981\u91cd\u5199Factory2\u7684onCreateView\u903b\u8f91\uff0c\u5c31\u4e0d\u5fc5\u7406\u4f1aFactory\u7684\u91cd\u5199\u65b9\u6cd5\u4e86\n     *\n     * @param name\n     * @param context\n     * @param attrs\n     * @return\n     *\/\n    @Override\n    public View onCreateView(String name, Context context, AttributeSet attrs) {\n        return null;\n    }\n\n    \/**\n     * @param parent\n     * @param name\n     * @param context\n     * @param attrs\n     * @return\n     *\/\n    @Override\n    public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {\n\n        \/\/ TODO: \u5173\u952e\u70b91\uff1a\u6267\u884c\u7cfb\u7edf\u4ee3\u7801\u91cc\u7684\u521b\u5efaView\u7684\u8fc7\u7a0b\uff0c\u6211\u4eec\u53ea\u662f\u60f3\u52a0\u5165\u81ea\u5df1\u7684\u601d\u60f3\uff0c\u5e76\u4e0d\u662f\u8981\u5168\u76d8\u63a5\u7ba1\n        View view = mDelegate.createView(parent, name, context, attrs); \/\/\u7cfb\u7edf\u521b\u5efa\u51fa\u6765\u7684\u65f6\u5019\u6709\u53ef\u80fd\u4e3a\u7a7a\uff0c\u4f60\u95ee\u4e3a\u5565\uff1f\u8bf7\u5168\u6587\u641c\u7d22 \u201c\u6807\u8bb0\u6807\u8bb0\uff0c\u56e0\u4e3a\u201d \u4f60\u4f1a\u627e\u5230\u4f60\u8981\u7684\u7b54\u6848\n        if (view == null) { \/\/\u4e07\u4e00\u7cfb\u7edf\u521b\u5efa\u51fa\u6765\u662f\u7a7a\uff0c\u90a3\u4e48\u6211\u4eec\u6765\u8865\u6551\n            mConstructorArgs[0] = context;\n            try {\n                if (-1 == name.indexOf(&#039;.&#039;)) { \/\/\u4e0d\u5305\u542b. \u8bf4\u660e\u4e0d\u5e26\u5305\u540d\uff0c\u90a3\u4e48\u6211\u4eec\u5e2e\u4ed6\u52a0\u4e0a\u5305\u540d\n                    view = createViewByPrefix(context, name, prefixs, attrs);\n                } else { \/\/\u5305\u542b. \u8bf4\u660e \u662f\u6743\u9650\u5b9a\u540d\u7684view name\uff0c\n                    view = createViewByPrefix(context, name, null, attrs);\n                }\n            } catch (Exception e) {\n                e.printStackTrace();\n            }\n        }\n\n        \/\/TODO: \u5173\u952e\u70b92 \u6536\u96c6\u9700\u8981\u6362\u80a4\u7684View\n        collectSkinView(context, attrs, view);\n\n        return view;\n    }\n\n    \/**\n     * TODO: \u6536\u96c6\u9700\u8981\u6362\u80a4\u7684\u63a7\u4ef6\n     * \u6536\u96c6\u7684\u65b9\u5f0f\u662f\uff1a\u901a\u8fc7\u81ea\u5b9a\u4e49\u5c5e\u6027isSupport\uff0c\u4ece\u521b\u5efa\u51fa\u6765\u7684\u5f88\u591aView\u4e2d\uff0c\u627e\u5230\u652f\u6301\u6362\u80a4\u7684\u90a3\u4e9b\uff0c\u4fdd\u5b58\u5230map\u4e2d\n     *\/\n    private void collectSkinView(Context context, AttributeSet attrs, View view) {\n        \/\/ \u83b7\u53d6\u6211\u4eec\u81ea\u5df1\u5b9a\u4e49\u7684\u5c5e\u6027\n        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Skinable);\n        boolean isSupport = a.getBoolean(R.styleable.Skinable_isSupport, false);\n        if (isSupport) { \/\/\u627e\u5230\u652f\u6301\u6362\u80a4\u7684view\n            final int Len = attrs.getAttributeCount();\n            HashMap&lt;String, String&gt; attrMap = new HashMap&lt;&gt;();\n            for (int i = 0; i &lt; Len; i++) { \/\/\u904d\u5386\u6240\u6709\u5c5e\u6027\n                String attrName = attrs.getAttributeName(i);\n                String attrValue = attrs.getAttributeValue(i);\n                attrMap.put(attrName, attrValue); \/\/\u5168\u90e8\u5b58\u8d77\u6765\n            }\n\n            SkinView skinView = new SkinView();\n            skinView.view = view;\n            skinView.attrsMap = attrMap;\n            listCacheSkinView.add(skinView);\/\/\u5c06\u53ef\u6362\u80a4\u7684view\uff0c\u653e\u5230listCacheSkinView\u4e2d\n        }\n\n    }\n\n    \/**\n     * \u516c\u5f00\u7ed9\u5916\u754c\u7684\u6362\u80a4\u5165\u53e3\n     *\/\n    public void changeSkin() {\n        for (SkinView skinView : listCacheSkinView) {\n            skinView.changeSkin();\n        }\n    }\n\n    static class SkinView {\n        View view;\n        HashMap&lt;String, String&gt; attrsMap;\n\n        \/**\n         * \u771f\u6b63\u7684\u6362\u80a4\u64cd\u4f5c\n         *\/\n        public void changeSkin() {\n            if (!TextUtils.isEmpty(attrsMap.get(&quot;background&quot;))) { \/\/\u5c5e\u6027\u540d, \u4f8b\u5982 background, text, textColor....\n                Log.i(&quot;yezhou&quot;, view.getClass().getSimpleName() + &quot;.background: &quot; + attrsMap.get(&quot;background&quot;));  \/\/@2131099735\n                int bgId = Integer.parseInt(attrsMap.get(&quot;background&quot;).substring(1)); \/\/\u5c5e\u6027\u503c, R.id.XXX, int\u7c7b\u578b\n                \/\/\u8fd9\u4e2a\u503c\uff0c\u5728app\u7684\u4e00\u6b21\u8fd0\u884c\u4e2d\uff0c\u4e0d\u4f1a\u53d1\u751f\u53d8\u5316\n                String attrType = view.getResources().getResourceTypeName(bgId); \/\/\u5c5e\u6027\u7c7b\u522b: \u6bd4\u5982 drawable,color\n                Log.i(&quot;yezhou&quot;, view.getClass().getSimpleName() + &quot;.attrType: &quot; + attrType);\n                if (TextUtils.equals(attrType, &quot;drawable&quot;)) { \/\/\u533a\u5206drawable\u548ccolor\n                    view.setBackgroundDrawable(SkinEngine.getInstance().getDrawable(bgId)); \/\/\u52a0\u8f7d\u5916\u90e8\u8d44\u6e90\u7ba1\u7406\u5668, \u62ff\u5230\u5916\u90e8\u8d44\u6e90\u7684drawable\n                } else if (TextUtils.equals(attrType, &quot;color&quot;)) {\n                    view.setBackgroundColor(SkinEngine.getInstance().getColor(bgId));\n                }\n            }\n\n            if (view instanceof TextView) {\n                if (!TextUtils.isEmpty(attrsMap.get(&quot;textColor&quot;))) {\n                    int textColorId = Integer.parseInt(attrsMap.get(&quot;textColor&quot;).substring(1));\n                    ((TextView) view).setTextColor(SkinEngine.getInstance().getColor(textColorId));\n                }\n            }\n\n            \/\/\u90a3\u4e48\u5982\u679c\u662f\u81ea\u5b9a\u4e49\u7ec4\u4ef6\u5462\n            if (view instanceof ZeroView) {\n                \/\/\u90a3\u4e48\u8fd9\u6837\u4e00\u4e2a\u5bf9\u8c61, \u8981\u6362\u80a4, \u5c31\u8981\u5199\u9488\u5bf9\u6027\u7684\u65b9\u6cd5\u4e86, \u6bcf\u4e00\u4e2a\u63a7\u4ef6\u9700\u8981\u7528\u4ec0\u4e48\u6837\u7684\u65b9\u5f0f\u53bb\u6362, \u5c24\u5176\u662f\u90a3\u79cd, \u81ea\u5b9a\u4e49\u7684\u5c5e\u6027, \u600e\u4e48\u53bbset\n                \/\/\u8fd9\u5c31\u5bf9\u5f00\u53d1\u4eba\u5458\u8981\u6c42\u6bd4\u8f83\u9ad8\u4e86, \u800c\u4e14\u8fd9\u4e2a\u6362\u80a4\u63a5\u53e3\u8fd8\u8981\u66b4\u9732\u7ed9\u81ea\u5b9a\u4e49View\u7684\u5f00\u53d1\u4eba\u5458,\u4ed6\u4eec\u53bb\u5b9a\u4e49\n                \/\/ ....\n            }\n        }\n    }\n\n    \/**\n     * \u6240\u8c13hook, \u8981\u61c2\u6e90\u7801, \u61c2\u4e86\u4e4b\u540e\u518d\u52ab\u6301\u7cfb\u7edf\u903b\u8f91, \u52a0\u5165\u81ea\u5df1\u7684\u903b\u8f91\n     * \u65e2\u7136\u61c2\u4e86, \u7cfb\u7edf\u7684\u6709\u4e9b\u4ee3\u7801, \u76f4\u63a5\u62ff\u8fc7\u6765\u7528, \u4e5f\u65e0\u53ef\u539a\u975e\n     *\/\n    \/\/*******************************\u4e0b\u9762\u4e00\u5927\u7247\uff0c\u90fd\u662f\u4ece\u6e90\u7801\u91cc\u9762\u6284\u8fc7\u6765\u7684\uff0c\u5e76\u4e0d\u662f\u6211\u81ea\u4e3b\u8bbe\u8ba1******************************\n    \/\/ \u4f60\u95ee\u6211\u6284\u7684\u54ea\u91cc\u7684\uff1f\u5230 AppCompatViewInflater\u7c7b\u6e90\u7801\u91cc\u9762\u53bb\u641c\u7d22\uff1aview = createViewFromTag(context, name, attrs);\n    static final Class&lt;?&gt;[] mConstructorSignature = new Class[]{ Context.class, AttributeSet.class };\n    final Object[] mConstructorArgs = new Object[2];\/\/View\u7684\u6784\u9020\u51fd\u6570\u76842\u4e2a&quot;\u5b9e&quot;\u53c2\u5bf9\u8c61\n    private static final HashMap&lt;String, Constructor&lt;? extends View&gt;&gt; sConstructorMap = new HashMap&lt;String, Constructor&lt;? extends View&gt;&gt;();\/\/\u7528\u6620\u5c04\uff0c\u5c06View\u7684\u53cd\u5c04\u6784\u9020\u51fd\u6570\u90fd\u5b58\u8d77\u6765\n    static final String[] prefixs = new String[]{ \/\/\u5b89\u5353\u91cc\u9762\u63a7\u4ef6\u7684\u5305\u540d\uff0c\u5c31\u8fd9\u4e483\u79cd,\u8fd9\u4e2a\u53d8\u91cf\u662f\u4e3a\u4e86\u4e0b\u9762\u4ee3\u7801\u91cc\uff0c\u53cd\u5c04\u521b\u5efa\u7c7b\u7684class\u800c\u9884\u5907\u7684\n            &quot;android.widget.&quot;,\n            &quot;android.view.&quot;,\n            &quot;android.webkit.&quot;\n    };\n\n    \/**\n     * \u53cd\u5c04\u521b\u5efaView\n     *\n     * @param context\n     * @param name\n     * @param prefixs\n     * @param attrs\n     * @return\n     *\/\n    private final View createViewByPrefix(Context context, String name, String[] prefixs, AttributeSet attrs) {\n\n        Constructor&lt;? extends View&gt; constructor = sConstructorMap.get(name);\n        Class&lt;? extends View&gt; clazz = null;\n\n        if (constructor == null) {\n            try {\n                if (prefixs != null &amp;&amp; prefixs.length &gt; 0) {\n                    for (String prefix : prefixs) {\n                        clazz = context.getClassLoader().loadClass(\n                                prefix != null ? (prefix + name) : name).asSubclass(View.class);\/\/\u63a7\u4ef6\n                        if (clazz != null) break;\n                    }\n                } else {\n                    if (clazz == null) {\n                        clazz = context.getClassLoader().loadClass(name).asSubclass(View.class);\n                    }\n                }\n                if (clazz == null) {\n                    return null;\n                }\n                constructor = clazz.getConstructor(mConstructorSignature);\/\/\u62ff\u5230 \u6784\u9020\u65b9\u6cd5\uff0c\n            } catch (Exception e) {\n                e.printStackTrace();\n                return null;\n            }\n            constructor.setAccessible(true);\/\/\n            sConstructorMap.put(name, constructor);\/\/\u7136\u540e\u7f13\u5b58\u8d77\u6765\uff0c\u4e0b\u6b21\u518d\u7528\uff0c\u5c31\u76f4\u63a5\u4ece\u5185\u5b58\u4e2d\u53bb\u53d6\n        }\n        Object[] args = mConstructorArgs;\n        args[1] = attrs;\n        try {\n            \/\/\u901a\u8fc7\u53cd\u5c04\u521b\u5efaView\u5bf9\u8c61\n            final View view = constructor.newInstance(args);\/\/\u6267\u884c\u6784\u9020\u51fd\u6570\uff0c\u62ff\u5230View\u5bf9\u8c61\n            return view;\n        } catch (Exception e) {\n            e.printStackTrace();\n        }\n        return null;\n    }\n\n}<\/code><\/pre>\n<p>\u81ea\u5b9a\u4e49View<\/p>\n<pre><code class=\"language-java\">package cn.appblog.skindemo.view;\n\nimport android.content.Context;\nimport android.util.AttributeSet;\nimport android.view.View;\n\npublic class ZeroView extends View {\n    public ZeroView(Context context) {\n        super(context);\n    }\n\n    public ZeroView(Context context, AttributeSet attrs) {\n        super(context, attrs);\n    }\n\n    public ZeroView(Context context, AttributeSet attrs, int defStyleAttr) {\n        super(context, attrs, defStyleAttr);\n    }\n}<\/code><\/pre>\n<h3>\u5173\u952e\u7c7b SkinEngine<\/h3>\n<pre><code class=\"language-java\">package cn.appblog.skindemo.skin;\n\nimport android.content.Context;\nimport android.content.pm.PackageInfo;\nimport android.content.pm.PackageManager;\nimport android.content.res.AssetManager;\nimport android.content.res.Resources;\nimport android.graphics.drawable.Drawable;\nimport android.util.Log;\n\nimport androidx.core.content.ContextCompat;\n\nimport java.io.File;\nimport java.lang.reflect.Method;\n\npublic class SkinEngine {\n\n    \/\/\u5355\u4f8b\n    private final static SkinEngine instance = new SkinEngine();\n\n    public static SkinEngine getInstance() {\n        return instance;\n    }\n\n    private SkinEngine() {\n    }\n\n    public void init(Context context) {\n        mContext = context.getApplicationContext();\n        \/\/\u4f7f\u7528application\u7684\u76ee\u7684\u662f\uff0c\u5982\u679c\u4e07\u4e00\u4f20\u8fdb\u6765\u7684\u662fActivity\u5bf9\u8c61\n        \/\/\u90a3\u4e48\u5b83\u88ab\u9759\u6001\u5bf9\u8c61instance\u6240\u6301\u6709\uff0c\u8fd9\u4e2aActivity\u5c31\u65e0\u6cd5\u91ca\u653e\u4e86\n    }\n\n    private Resources mOutResource; \/\/ TODO: \u8d44\u6e90\u7ba1\u7406\u5668\n    private Context mContext; \/\/\u4e0a\u4e0b\u6587\n    private String mOutPkgName; \/\/ TODO: \u5916\u90e8\u8d44\u6e90\u5305\u7684packageName\n\n    \/**\n     * TODO: \u52a0\u8f7d\u5916\u90e8\u8d44\u6e90\u5305\n     *\/\n    public void load(final String path) { \/\/path \u662f\u5916\u90e8\u4f20\u5165\u7684apk\u6587\u4ef6\u540d\n        File file = new File(path);\n        if (!file.exists()) {\n            return;\n        }\n        \/\/\u53d6\u5f97PackageManager\u5f15\u7528\n        PackageManager mPm = mContext.getPackageManager();\n        \/\/\u201c\u68c0\u7d22\u5728\u5305\u5f52\u6863\u6587\u4ef6\u4e2d\u5b9a\u4e49\u7684\u5e94\u7528\u7a0b\u5e8f\u5305\u7684\u603b\u4f53\u4fe1\u606f\u201d, \u8bf4\u4eba\u8bdd, \u5916\u754c\u4f20\u5165\u4e86\u4e00\u4e2aapk\u7684\u6587\u4ef6\u8def\u5f84, \u8fd9\u4e2a\u65b9\u6cd5, \u62ff\u5230\u8fd9\u4e2aapk\u7684\u5305\u4fe1\u606f,\u8fd9\u4e2a\u5305\u4fe1\u606f\u5305\u542b\u4ec0\u4e48\uff1f\n        PackageInfo mInfo = mPm.getPackageArchiveInfo(path, PackageManager.GET_ACTIVITIES);\n        mOutPkgName = mInfo.packageName; \/\/\u5148\u628a\u5305\u540d\u5b58\u8d77\u6765\n        AssetManager assetManager;\/\/\u8d44\u6e90\u7ba1\u7406\u5668\n        try {\n            \/\/TODO: \u5173\u952e\u6280\u672f\u70b93 \u901a\u8fc7\u53cd\u5c04\u83b7\u53d6AssetManager \u7528\u6765\u52a0\u8f7d\u5916\u9762\u7684\u8d44\u6e90\u5305\n            assetManager = AssetManager.class.newInstance(); \/\/\u53cd\u5c04\u521b\u5efaAssetManager\u5bf9\u8c61, \u4e3a\u4f55\u8981\u53cd\u5c04? \u4f7f\u7528\u53cd\u5c04, \u662f\u56e0\u4e3a\u4ed6\u8fd9\u4e2a\u7c7b\u5185\u90e8\u7684addAssetPath\u65b9\u6cd5\u662fhide\u72b6\u6001\n            \/\/addAssetPath\u65b9\u6cd5\u53ef\u4ee5\u52a0\u8f7d\u5916\u90e8\u7684\u8d44\u6e90\u5305\n            Method addAssetPath = assetManager.getClass().getMethod(&quot;addAssetPath&quot;, String.class); \/\/\u4e3a\u4ec0\u4e48\u8981\u53cd\u5c04\u6267\u884c\u8fd9\u4e2a\u65b9\u6cd5? \u56e0\u4e3a\u5b83\u662fhide\u7684, \u4e0d\u76f4\u63a5\u5bf9\u5916\u5f00\u653e, \u53ea\u80fd\u53cd\u5c04\u8c03\u7528\n            addAssetPath.invoke(assetManager, path); \/\/\u53cd\u5c04\u6267\u884c\u65b9\u6cd5\n            mOutResource = new Resources(assetManager, \/\/\u53c2\u65701\uff0c\u8d44\u6e90\u7ba1\u7406\u5668\n                    mContext.getResources().getDisplayMetrics(), \/\/\u8fd9\u4e2a\u597d\u50cf\u662f\u5c4f\u5e55\u53c2\u6570\n                    mContext.getResources().getConfiguration()); \/\/\u8d44\u6e90\u914d\u7f6e\n            \/\/\u6700\u7ec8\u521b\u5efa\u51fa\u4e00\u4e2a&quot;\u5916\u90e8\u8d44\u6e90\u5305&quot;mOutResource, \u5b83\u7684\u5b58\u5728, \u5c31\u662f\u8981\u8ba9\u6211\u4eec\u7684app\u6709\u80fd\u529b\u52a0\u8f7d\u5916\u90e8\u7684\u8d44\u6e90\u6587\u4ef6\n        } catch (Exception e) {\n            e.printStackTrace();\n        }\n    }\n\n    \/**\n     * \u63d0\u4f9b\u5916\u90e8\u8d44\u6e90\u5305\u91cc\u9762\u7684\u989c\u8272\n     * @param resId\n     * @return\n     *\/\n    public int getColor(int resId) {\n        if (mOutResource == null) {\n            return resId;\n        }\n        String resName = mOutResource.getResourceEntryName(resId);\n        int outResId = mOutResource.getIdentifier(resName, &quot;color&quot;, mOutPkgName);\n        if (outResId == 0) {\n            return resId;\n        }\n        return mOutResource.getColor(outResId);\n    }\n\n    \/**\n     * \u63d0\u4f9b\u5916\u90e8\u8d44\u6e90\u5305\u91cc\u7684\u56fe\u7247\u8d44\u6e90\n     * @param resId\n     * @return\n     *\/\n    public Drawable getDrawable(int resId) { \/\/\u83b7\u53d6\u56fe\u7247\n        if (mOutResource == null) {\n            return ContextCompat.getDrawable(mContext, resId);\n        }\n        String resName = mOutResource.getResourceEntryName(resId);\n        int outResId = mOutResource.getIdentifier(resName, &quot;drawable&quot;, mOutPkgName);\n        Log.i(&quot;yezhou&quot;, &quot;SkinEngine.resId: &quot; + resId);\n        Log.i(&quot;yezhou&quot;, &quot;SkinEngine.outResId: &quot; + outResId);\n        if (outResId == 0) {\n            return ContextCompat.getDrawable(mContext, resId);\n        }\n        return mOutResource.getDrawable(outResId);\n    }\n\n    \/\/..... \u8fd9\u91cc\u8fd8\u53ef\u4ee5\u63d0\u4f9b\u5916\u90e8\u8d44\u6e90\u5305\u91cc\u7684String, font\u7b49\u7b49\u7b49\uff0c\u53ea\u4e0d\u8fc7\u8981\u624b\u52a8\u5199\u4ee3\u7801\u6765\u5b9e\u73b0getXX\u65b9\u6cd5\n}<\/code><\/pre>\n<h3>\u5173\u952e\u7c7b\u7684\u8c03\u7528\u65b9\u5f0f<\/h3>\n<h4>\u521d\u59cb\u5316&quot;\u6362\u80a4\u5f15\u64ce&quot;<\/h4>\n<pre><code class=\"language-java\">package cn.appblog.skindemo.app;\n\nimport android.app.Application;\n\nimport cn.appblog.skindemo.skin.SkinEngine;\n\npublic class MyApp extends Application {\n\n    @Override\n    public void onCreate() {\n        super.onCreate();\n        \/\/\u521d\u59cb\u5316\u6362\u80a4\u5f15\u64ce\n        SkinEngine.getInstance().init(this);\n    }\n}<\/code><\/pre>\n<h4>\u52ab\u6301\u7cfb\u7edf\u521b\u5efaview\u7684\u8fc7\u7a0b<\/h4>\n<pre><code class=\"language-java\">package cn.appblog.skindemo.base;\n\nimport android.os.Bundle;\nimport android.os.Environment;\nimport android.util.Log;\nimport android.view.LayoutInflater;\n\nimport androidx.appcompat.app.AppCompatActivity;\n\nimport java.io.File;\n\nimport cn.appblog.skindemo.skin.SkinEngine;\nimport cn.appblog.skindemo.skin.SkinFactory;\n\n\/**\n * \u628a\u6362\u80a4\u7684\u529f\u80fd\u5b9a\u4e49\u5728\u8fd9\u91cc\n *\/\npublic class BaseActivity extends AppCompatActivity {\n    protected static String[] skins = new String[]{&quot;blue_skin_plugin.apk&quot;, &quot;orange_skin_plugin.apk&quot;};\n\n    protected static String mCurrentSkin = null;\n\n    private SkinFactory mSkinFactory;\n\n    private boolean ifAllowChangeSkin = true;\n\n    @Override\n    protected void onCreate(Bundle savedInstanceState) {\n        \/\/ TODO: \u5173\u952e\u70b91\uff1ahook\uff08\u52ab\u6301\uff09\u7cfb\u7edf\u521b\u5efaview\u7684\u8fc7\u7a0b\n        if (ifAllowChangeSkin) {\n            mSkinFactory = new SkinFactory();\n            mSkinFactory.setDelegate(getDelegate());\n            LayoutInflater layoutInflater = LayoutInflater.from(this);\n            Log.d(&quot;layoutInflaterTag&quot;, layoutInflater.toString());\n            layoutInflater.setFactory2(mSkinFactory);\n        }\n        super.onCreate(savedInstanceState);\n    }\n\n    \/**\n     * \u521b\u5efa\u5b8c\u6210\u4f46\u662f\u8fd8\u4e0d\u53ef\u4ee5\u4ea4\u4e92\n     *\/\n    @Override\n    protected void onStart() {\n        super.onStart();\n    }\n\n    \/**\n     * \u7b49\u63a7\u4ef6\u521b\u5efa\u5b8c\u6210\u5e76\u4e14\u53ef\u4ea4\u4e92\u4e4b\u540e\uff0c\u518d\u6362\u80a4\n     *\/\n    @Override\n    protected void onResume() {\n        super.onResume();\n        Log.d(&quot;changeTag&quot;, null == mCurrentSkin ? &quot;currentSkin\u662f\u7a7a&quot; : mCurrentSkin);\n\n        if (null != mCurrentSkin)\n            changeSkin(mCurrentSkin); \/\/ \u6362\u80a4\u64cd\u4f5c\u5fc5\u987b\u5728setContentView\u4e4b\u540e\n    }\n\n    \/**\n     * \u505a\u4e00\u4e2a\u5207\u6362\u65b9\u6cd5\n     *\n     * @return\n     *\/\n    protected String getPath() {\n        String path;\n        if (null == mCurrentSkin) {\n            path = skins[0];\n        } else if (skins[0].equals(mCurrentSkin)) {\n            path = skins[1];\n        } else if (skins[1].equals(mCurrentSkin)) {\n            path = skins[0];\n        } else {\n            return &quot;unknown skin&quot;;\n        }\n        return path;\n    }\n\n    protected void changeSkin(String path) {\n        if (ifAllowChangeSkin) {\n            File skinFile = new File(Environment.getExternalStorageDirectory(), path);\n            SkinEngine.getInstance().load(skinFile.getAbsolutePath());\n            mSkinFactory.changeSkin();\n            mCurrentSkin = path;\n        }\n    }\n}<\/code><\/pre>\n<h4>\u6267\u884c\u6362\u80a4\u64cd\u4f5c<\/h4>\n<pre><code class=\"language-java\">package cn.appblog.skindemo.ui;\n\nimport android.content.Intent;\nimport android.content.pm.PackageManager;\nimport android.os.Bundle;\nimport android.view.View;\n\nimport androidx.appcompat.app.AppCompatActivity;\nimport androidx.core.app.ActivityCompat;\n\nimport cn.appblog.skindemo.R;\nimport cn.appblog.skindemo.base.BaseActivity;\n\npublic class MainActivity extends BaseActivity {\n\n    private static final int REQUEST_EXTERNAL_STORAGE = 1;\n    private static String[] PERMISSIONS_STORAGE = {\n            &quot;android.permission.READ_EXTERNAL_STORAGE&quot;,\n            &quot;android.permission.WRITE_EXTERNAL_STORAGE&quot;}; \/\/\u5185\u5b58\u8bfb\u5199\u7684\u6743\u9650\uff0c\u52a8\u6001\u7533\u8bf7\n\n    @Override\n    protected void onCreate(Bundle savedInstanceState) {\n        super.onCreate(savedInstanceState);\n        setContentView(R.layout.activity_main);\n\n        findViewById(R.id.textView).setBackground(getDrawable(R.color.colorPrimary));\n\n        verifyStoragePermissions(this);\/\/\u7533\u8bf7\u6743\u9650\n        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {\n            @Override\n            public void onClick(View v) {\n                changeSkin(getPath());\n            }\n        });\n        findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {\n            @Override\n            public void onClick(View v) {\n                Intent intent = new Intent(MainActivity.this, Main2Activity.class);\n                startActivity(intent);\n            }\n        });\n    }\n\n    \/**\n     * \u7533\u8bf7\u6743\u9650\uff0c\u4e3a\u4e86\u8981\u628a\u5916\u90e8\u6587\u4ef6\u5199\u5165\u5230 \u624b\u673a\u5185\u5b58\u4e2d\n     *\n     * @param activity\n     *\/\n    public static void verifyStoragePermissions(AppCompatActivity activity) {\n        try {\n            \/\/\u68c0\u6d4b\u662f\u5426\u6709\u5199\u7684\u6743\u9650\n            int permission = ActivityCompat.checkSelfPermission(activity,PERMISSIONS_STORAGE[1]);\n            if (permission != PackageManager.PERMISSION_GRANTED) {\n                \/\/\u6ca1\u6709\u5199\u7684\u6743\u9650\uff0c\u53bb\u7533\u8bf7\u5199\u7684\u6743\u9650\uff0c\u4f1a\u5f39\u51fa\u5bf9\u8bdd\u6846\n                ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE);\n            }\n        } catch (Exception e) {\n            e.printStackTrace();\n        }\n    }\n}<\/code><\/pre>\n<pre><code class=\"language-java\">package cn.appblog.skindemo.ui;\n\nimport android.os.Bundle;\n\nimport cn.appblog.skindemo.R;\nimport cn.appblog.skindemo.base.BaseActivity;\n\npublic class Main2Activity extends BaseActivity {\n\n    @Override\n    protected void onCreate(Bundle savedInstanceState) {\n        super.onCreate(savedInstanceState);\n        setContentView(R.layout.activity_main2);\n    }\n\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4ea7\u54c1\u8981\u6c42app\u91cc\u9762\u7684\u56fe\u8868\u8981\u5b9e\u73b0\u767d\u5929\u9ed1\u591c\u6a21\u5f0f\u7684\u5207\u6362\uff0c\u4ee5\u6ee1\u8db3\u4e0d\u540c\u5149\u7ebf\u4e0b\u90fd\u80fd\u4fdd\u8bc1\u8db3\u591f\u7684\u56fe\u8868\u6e05\u6670\u5ea6\u3002\u600e\u4e48\u529e\uff1f\u53ef\u80fd\u89e3\u51b3\u7684 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[311],"tags":[310,469],"class_list":["post-1833","post","type-post","status-publish","format-standard","hentry","category-android-advance","tag-hook","tag-469"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1833","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1833"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1833\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1833"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1833"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1833"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}